我正在使用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"
}
}
答案 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/";
看起来像一个糟糕的网址。这是我的建议: