Getting a JSONException: end of input at character 0
You are probably getting a blank response. Its not null but the response is empty. So you are getting this error and not a Nullpointer exception
JSONException: End of input at character 0
You are probably getting a blank response. Its not null but the jsontext is empty. So you are getting this error and not a Nullpointer exception
Are you sending right parameters to server.Also Check url respond to POST requests or not.
org.json.JSONException: End of input at character 0 of in android studio
private void execute(final Context context)
{
String url = UrlManager.getUrl(context, R.string.signup_url);
User user = User.getInstance();
try {
url += "&name=" + mFirstName + "&last=" + mLastName + "&mob=" + mMobile +
"&email=" + mEmail+
"&pass=" + UrlManager.prepareUrlPart(mPassword);
} catch (Exception e) {
e.printStackTrace();
}
final ProgressDialog pDialog;
pDialog = new ProgressDialog(context);
pDialog.setMessage("Loading...");
pDialog.setCancelable(false);
pDialog.show();
Response.Listener<JSONObject> listener = new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response)
{
try
{
JSONObject userObj = response;
userInfo = new UserInfo();
userInfo.LoadItem(context,
userObj);
String state = userObj.getString("State");
if (state .equals("1")) {
// Load user data from json and save them into local database
// User user = User.getInstance();
// user.loadFromJSon(response);
/// user.saveUser();
onResult(true);
} else {
onResult(false);
}
}
catch (Exception e)
{
Log.d("Error", e.getMessage());
e.printStackTrace();
}
pDialog.dismiss();
}
};
Response.ErrorListener errorListener = new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
Toast.makeText(context, "Error", Toast.LENGTH_LONG).show();
pDialog.dismiss();
}
};
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url,null, listener, errorListener);
MyApplication.getInstance().addToRequestQueue(request);
}
Volley JSONException: End of input at character 0 of
I also have encountered this issue.
It's not necessarily true that this is because a problem on your server side - it simply means that the response of the JsonObjectRequest
is empty.
It could very well be that the server should be sending you content, and the fact that its response is empty is a bug. If, however, this is how the server is supposed to behave, then to solve this issue, you will need to change how JsonObjectRequest parses its response, meaning creating a subclass of JsonObjectRequest
, and overriding the parseNetworkResponse
to the example below.
@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));
JSONObject result = null;
if (jsonString != null && jsonString.length() > 0)
result = new JSONObject(jsonString);
return Response.success(result,
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
}
Keep in mind that with this fix, and in the event of an empty response from the server, the request callback will return a null reference in place of the JSONObject
.
org.json.JSONException: End of input at character 0 of
Try to use BufferedReader :
BufferedReader r = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder result = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
result.append(line);
}
org.json.JSONException: End of input at character 0 of
Based on the error, you aren't getting JSON back, which likely means you are getting some HTTP error.
You could more easily see that if you Log
the value of result
before trying to parse it.
However, you should be aware of this.
"To access the API you need to sign up for an API key if you are on a free or paid plan."
- http://openweathermap.org/api
See here to use the API key
In other words, go get an API key, then append it to your URL. For example
String API_KEY = "XXXXxxxx";
String url = "http://.../weather?q=" + city + "&APPID=" + API_KEY;
org.json.JSONException: End of input at character 0 of
Answer : Response object should of Generic type - ResponseBody
.
See below Correct code for reference.
Now response.body()
method will return object ResponseBody
i.e.
ResponseBody rb = response.body();
rb.string();
Here ResponseBody have method string()
which returns String
object but internally string()
method calls Util.closeQuietly(source);
which makes response empty once method string()
gets called.
Just remove Log.d(TAG, response.body().string());
and follow below code.
Reference - okhttp3.ResponseBody.java
error : org.json.JSONException: End of input at character 0
Correct code :
@Override
public void onResponse(Call call, Response<ResponseBody> response) throws IOException {
if (response.isSuccessful()) {
String remoteResponse=response.body().string();
Log.d(TAG, remoteResponse);
try {
JSONObject forecast = new JSONObject(remoteResponse);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
org.json.JSONException: End of input at character 0 of, android
As you have seen in your logcat 1576-1576/com.example.neshat.androidhive2 D/RegisterActivity﹕ Register Response:
which is the result of Log.d(TAG, "Register Response: " + response.toString());
, response
is an empty string, so your app will get org.json.JSONException: End of input at character 0 of
at the line JSONObject jObj = new JSONObject(response);
.
You should check response
is not null and not an empty string first.
Related Topics
What Is The Easiest Way to Use Svg Images in Android
Why Bitmap to Base64 String Showing Black Background on Webview in Android
Shared Preferences Reset Data When App Is Force Closed or Device Is Restarted
Activity.Finish() Called But Activity Stays Loaded in Memory
How to Reduce Latency in Mediacodec Video/Avc Decoding
How to Create a Looper Thread, Then Send It a Message Immediately
How to Programmatically Include Layout in Android
How to Create a Service Which Continuously Monitors App Usage Information
Widgets Don't Respond When Re-Added Through Code
Firebase Changing Layout of Child Data Information in Android
How to Save Image Taken from Camera and Show It to Listview - Crashes with "Illegalstateexception"
How to Develop Android App Completely Using Python
Eclipse Hangs at The Android Sdk Content Loader
How Many Activities Vs Fragments
Placeholder Text for an Input Type="Number" Does Not Show in Webkit Ics
Android: How to Return Async JSONobject from Method Using Volley