获取错误org.json.JSONException:Value <pre of="" type="" java.lang.string="" cannot="" be="" converted="" to="" jsonobject=""

时间:2016-08-01 10:22:25

标签: android json android-volley

="" I want to access my Android apps through Web-Service. but getting error in my android app ,

i am using volley & POST method for login.. `public class Main extends AppCompatActivity implements View.OnClickListener { public static final String LOGIN_URL = "http://10.54.103.8:4067/evivaservices/Webservices/login";

public static final String KEY_USERNAME="username";
public static final String KEY_PASSWORD="password";

private EditText editTextUsername;
private EditText editTextPassword;
private Button buttonLogin;

private String username;
private String password;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login_layout);

    editTextUsername = (EditText) findViewById(R.id.username1);
    editTextPassword = (EditText) findViewById(R.id.password1);

    buttonLogin = (Button) findViewById(R.id.login_button);

    buttonLogin.setOnClickListener(this);
}
private void userLogin() {
    username = editTextUsername.getText().toString().trim();
    password = editTextPassword.getText().toString().trim();

    StringRequest stringRequest = new StringRequest(Request.Method.POST, LOGIN_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try
                    {
                        JSONObject jsonObject = new JSONObject(response);
                        Next();
                    }
                    catch(JSONException e)
                    {
                    e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(getApplicationContext(),error.toString(), Toast.LENGTH_LONG ).show();
                }
            }){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String,String> map = new HashMap<String,String>();
            map.put(KEY_USERNAME,username);
            map.put(KEY_PASSWORD,password);
            return map;
        }
    };

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

private void Next(){
    Intent intent = new Intent(this, HomeScreen.class);
    intent.putExtra(KEY_USERNAME, username);
    startActivity(intent);
}
@Override
public void onClick(View v)
{
userLogin();
}

} `

JSON DATA


{
  "id": 31,
  "username": "andrew.cope@services.co.in",
  "user_image": "http:\/\/103.54.103.8:4067\/evivaservices\/img\/profile_31.png",
  "err-code": 0
}

`

3 个答案:

答案 0 :(得分:0)

您好亲爱的,您在解析响应时犯了一个错误

 JSONArray jsonArray=jsonObject.getJSONArray("err-code")

删除该行,然后再次解析。

答案 1 :(得分:0)

在你的JSON DATA中,我找不到数组,所以你不应该使用JsonArray,你可以在代码中删除这一行:JSONArray jsonArray=jsonObject.getJSONArray("err-code").

答案 2 :(得分:0)

请将您的StringRequest更改为JsonRequest,如下所示:

 JsonRequest jsonRequest = new JsonRequest(Request.Method.POST, LOGIN_URL, new Response.Listener<>() {
        @Override
        public void onResponse(Object response) {
            try {
                Next();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getApplicationContext(), error.toString(), Toast.LENGTH_LONG).show();
        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> map = new HashMap<String, String>();
            map.put(KEY_USERNAME, username);
            map.put(KEY_PASSWORD, password);
            return map;
        }

        @Override
        protected Response parseNetworkResponse(NetworkResponse response) {
            return null;
        }

        @Override
        public int compareTo(Object another) {
            return 0;
        }
    };

谢谢。