私はJsonの解析にokhttpライブラリを使用しています。Json Webserviceの不適切な応答の取得
私は私のログインWebサービスについては、以下のように私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();
}
}
}
}
しかし、Unfortunetly応答の下に取得:私は適切な応答を取得しています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)
:
<!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; }
とExeption郵便配達員に。 私の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();
}
}
間違いかもしれませんか?助けてください 。
=== >> EDIT:
は、以下のように郵便配達で適切な応答を取得する:
{
"status": 1,
"message": "Login successful",
"data": {
"name": "subhu",
"mobile_number": "7777777777",
"email": "[email protected]",
"user_id": "20"
}
}
POSTでサーバーが予期しているデータの種類は何ですか? FormBodyではなくJSONではないのは確かですか? – airowe
私はポストリクエストにフォームボディを送っています。 –
あなたは郵便配達員とあなたのAsyncTaskで使用したのと同じURLですか? – Ibrahim