获得Json webservice的不正确响应

时间:2017-06-23 19:23:22

标签: android json httpresponse okhttp

我正在使用okhttp库进行Json解析。

我已经为我的Login Webservice创建了我的AsyncTask:

class LoginAsyncTask extends AsyncTask<Void, Void, Void> {
    private ProgressDialog dialog;
    private String mstrLoginResponse="";

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        try {
            dialog = new ProgressDialog(SignInActivity.this);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
            dialog.setCancelable(false);
            if (!dialog.isShowing()) {
                dialog.show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    protected Void doInBackground(Void... params) {
        try {
            RequestBody formBody = new FormEncodingBuilder()
                    .add("mobile_number", strPhone)
                    .add("password", strPassword)
                    .build();
            mstrLoginResponse = HttpUtils.postRun("login", formBody);
            Constant.displayLogE(">>>TT : ",""+mstrLoginResponse);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
        if (mstrLoginResponse != null) {
            try {
                JSONObject jsonRegister = new JSONObject(mstrLoginResponse);

                int strLoginStatus = jsonRegister.getInt("status");
                String strMessage = jsonRegister.getString("message");
                if (strLoginStatus == 1) {
                    JSONObject jsonUserId = jsonRegister.getJSONObject("data");
                    String strUserId = jsonUserId.getString("user_id");
                    AppSettings.setPref(SignInActivity.this, USER_ID, String.valueOf(strUserId));
                    startActivity(new Intent(SignInActivity.this, MainActivity.class));
                    finish();
                    Constant.displayToast(SignInActivity.this, "" + strMessage);
                } else {
                    ApiUtils.showAlertDialogue(SignInActivity.this, "" + strMessage);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

但是,不幸的是低于回应:

<!DOCTYPE html>
                                                 <html lang="en">
                                                 <head>
                                                 <meta charset="utf-8">
                                                 <title>404 Page Not Found</title>
                                                 <style type="text/css">

                                                 ::selection { background-color: #E13300; color: white; }
                                                 ::-moz-selection { background-color: #E13300; color: white; }

和执行:org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject 06-24 00:41:03.314 349-349/com.roadysmart W/System.err: at org.json.JSON.typeMismatch(JSON.java:111)

我在Postman得到了适当的回应。 我的HttpUtils类如下:`

    public class HttpUtils {
    /*public static final MediaType JSON
            = MediaType.parse("application/json; charset=utf-8");*/
  public static String BASE_URL = "http://temp.in/projects/temp/";

    public static OkHttpClient client = new OkHttpClient();

    public static String getRun(String url) throws IOException {
        Log.d("URL===>", url);
        Request request = new Request.Builder()
                .url(url)
                .build();
        Response response = client.newCall(request).execute();
        return response.body().string();
    }

    public static String postRun(String type, RequestBody formBody) throws IOException {
        String str = "";
        Request request = new Request.Builder()
                .url(BASE_URL)
                .post(formBody)
                .build();
        Response response = client.newCall(request).execute();
        return response.body().string();
    }

}

可能是什么错误?请帮助。

===&gt;&gt;编辑:

在邮递员中得到适当的回应如下:

{
"status": 1,
"message": "Login successful",
"data": {
    "name": "subhu",
    "mobile_number": "7777777777",
    "email": "bhargavmodi777@gmail.com",
    "user_id": "20"
}

}

1 个答案:

答案 0 :(得分:1)

您的回复 - <title>404 Page Not Found</title>告诉我您的终端不存在,因为您输入的内容不正确或服务器根本没有运行。

您发布的异常与同一问题有关,它无法解析JSON,因为收到的网络电话不是application/json而只是原始HTML。

Exeption : org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot

据说,public static String BASE_URL = "http://temp.in/projects/temp/";看起来像一个糟糕的网址。这是我的建议:

  1. 首先在浏览器中手动加载该网址,您会看到收到客户来电的同一个HTML,会显示给您。
  2. 确保您正在运行服务器/服务或响应您的请求的任何内容。你说Postman有效,你使用了什么参数?你在跟什么沟通?
  3. 如果PostMan工作,并且邮递员从您的计算机上运行,​​则您的计算机已连接到我认为的本地网络。您的Android设备也在本地网络上吗?或者您使用的是小区服务(3G / LTE)。如果您是,那么除非您连接到VPN或其他东西,否则无法联系到您的终端。