这是程序,但获得JSON异常。
private void uploadImage(){
String uploadUrl = "http://...........";
StringRequest stringRequest = new StringRequest(Request.Method.POST, uploadUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
String Response = jsonObject.getString("response");
Toast.makeText(MainActivity.this,Response,Toast.LENGTH_SHORT).show();
mImage.setImageResource(0);
mImage.setVisibility(View.GONE);
mName.setText("");
mName.setVisibility(View.GONE);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("photo_one",imageString(bitmap));
params.put("mobile",mName.getText().toString().trim());
return params;
}
};
MySingleton.getInstance(MainActivity.this).addToRequestQue(stringRequest);
}
这是我得到的错误。
org.json.JSONException:at的字符0处的输入结束 org.json.JSONTokener.syntaxError(JSONTokener.java:449)at at org.json.JSONTokener.nextValue(JSONTokener.java:97)at org.json.JSONObject。(JSONObject.java:156)at org.json.JSONObject。(JSONObject.java:173)at com.example.athis.practiceproject4.MainActivity $ 1.onResponse(MainActivity.java:93)
哪个是JSONObject jsonObject = new JSONObject(response)
;线。
有什么问题?
答案 0 :(得分:2)
org.json.JSONException:at
的字符0处的输入结束
你的回复是空的。
答案 1 :(得分:1)
检查您的响应是否为错误 org.json.JSONException:at <0>的字符0处的输入结束表示您的响应字符串为null或0,请先检查您的响应字符串。
答案 2 :(得分:0)
尝试使用以下代码;
HashMap<String, String> params = new HashMap<>();
params.put("photo_one",imageString(bitmap));
params.put("mobile",mName.getText().toString().trim());
final ProgressDialog progressDialog = new ProgressDialog(LoginActivity.this);
progressDialog.setMessage("Fetching Data....");
progressDialog.show();
JsonObjectRequest req = new JsonObjectRequest(uploadUrl, new JSONObject(params),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
progressDialog.dismiss();
String success = (String) response.opt("success");
if (success.equals("1") )
{
// Do your Task here....
}
else
{
Toast.makeText(getApplicationContext(),"Invalid Username & Password...",Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(req);