类型为string的json错误值无法转换为jsonobject

时间:2016-05-15 12:28:14

标签: android json android-volley

我使用PHP网络服务制作登录页面。但是在JSON响应中,我有一个主JSON(check this link)之外的字符串。我尝试使用此link作为参考解析它,但我收到此JSON错误。任何人都可以帮助我在排球请求中解析这个JSON吗?



public class MainActivity extends AppCompatActivity {

    private EditText username, password;
    private Button login;

    private static final String LOGIN = "http://demo.example.net/login.php";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        username = (EditText)findViewById(R.id.ET1);
        password = (EditText)findViewById(R.id.ET2);
        login = (Button)findViewById(R.id.btn);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = username.getText().toString();
                String pass = password.getText().toString();

                if(!name.isEmpty() && !pass.isEmpty()){
                    attemptlogin();
                }
            }
        });
    }

    private void attemptlogin() {

        StringRequest stringRequest = new StringRequest(Request.Method.POST, LOGIN,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show();

                        try {
                            JSONObject jObj = new JSONObject(response);

                            JSONObject phone = jObj.getJSONObject("testtest123");

                            String status = phone.getString("success");

                            // Now check status value
                            if (status.equals("0")) {
                                Toast.makeText(getApplicationContext(), "There was some error! Please try again.", Toast.LENGTH_LONG).show();
                            } else if (status.equals("1")) {

                                Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_LONG).show();



                            //    startActivity(new Intent(getApplicationContext(), SecondActivity.class));
                             //   finish();



                            } else {
                                String errorMsg = jObj.getString("error_msg");
                                Toast.makeText(getApplicationContext(), errorMsg, Toast.LENGTH_LONG).show();
                            }
                        } catch (JSONException e) {
                            // JSON error
                            e.printStackTrace();
                            Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplicationContext(), "VolleyError" + error.toString(), Toast.LENGTH_LONG).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("username", username.getText().toString());
                params.put("password", password.getText().toString());
                return params;
            }

        };

        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        requestQueue.add(stringRequest);
    }
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

那是因为你试图将整个字符串转换为Json Object而无法完成。

在你的onResponse内试试这个

        int index=response.indexOf("{");
       String jsonString= st.substring(index);
       JSONObject jObj = new JSONObject(jsonString);
       String status = jObj.getString("success");

只有字符串以&#34; {&#34;。

开头时才能转换为JsonObject