【问题标题】:getting error org.json.JSONException: Value <pre of type java.lang.String cannot be converted to JSONObject得到错误 org.json.JSONException: Value <pre of type java.lang.String cannot be convert to JSONObject
【发布时间】:2016-12-06 09:22:35
【问题描述】:

我想通过 Web 服务访问我的 Android 应用程序。 但在我的 android 应用程序中出现错误,

我正在使用 volley 和 POST 方法进行登录。`public class Main 扩展 AppCompatActivity 实现 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
}

`

【问题讨论】:

  • 您应该使用JsonRequest 而不是StringRequest
  • 您的代码不应在第一个实例中编译,因为您缺少;。要解决您的问题,请删除行 JSONArray jsonArray=jsonObject.getJSONArray("err-code")
  • @chandeshwar Thakur:这解决了你的问题吗?
  • 是的兄弟...我解决了.this.by JsonObjectRequest 方法..thanx 建议我...但我不知道为什么我的声誉下降
  • @chandeshwar Thakur:那是因为有人否决了你的问题 :(

标签: android json android-volley


【解决方案1】:

请将您的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;
        }
    };

谢谢。

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 检查代码是否正确...在 JSONObject jsonObject = new JSONObject(response); 处出现错误
    • 请解释为什么这样可以解决问题
    • @AesSedai101 JSONArray 用于 JSON 数据,它被截断为“result”:[ {"value1": 1} , {"value2":2} , {"value3":3} ]。你知道 JSON 数据有数组。在这种情况下,您必须使用 JSONArray 类。
    【解决方案3】:

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

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

    删除上面的行,然后重新解析。

    【讨论】:

      猜你喜欢
      • 2013-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多