cakephp apiを使用してサーバーにデータを投稿しようとしています。ポストマンを使用して私のAPIは正常に動作しますが、Androidではいくつか問題があります。私はそれをasynctaskを使用していますが、私は投稿するjsonに問題があるかもしれないと思う。 私はandroidにサーバーにデータを投稿
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
ことを、この私のコードを私に言って、エラーメッセージが表示されました、あなたはそれで間違っているものを私に教えてくださいだろうか?
public class AsycTaskManager {
public static class AddApplication extends AsyncTask<String, Void, String> {
String result ;
@Override
protected String doInBackground(String... params) {
String addFlightPlanUrl = "http://xxx.xxx.xxx.xxx:8765/car/addapplication";
String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjIsImV4cCI6MTUwMTczOTkyNH0.5dwPuoScLdI3ivkuJ6afpmST6D5MwuoAcOzQ0EGBLAY";
String postData ;
URL object = null;
try {
object = new URL(addFlightPlanUrl);
HttpURLConnection con = (HttpURLConnection) object.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestProperty("Authorization", token);
con.setRequestProperty("Accept", "application/json");
con.setRequestProperty("Content-Type", "application/json");
con.setRequestMethod("POST");
JSONObject cred = new JSONObject();
cred.accumulate("code", "2");
cred.accumulate("name", "2");
OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
wr.write(cred.toString());
wr.flush();
wr.close();
StringBuilder sb = new StringBuilder();
int HttpResult = con.getResponseCode();
if (HttpResult == HttpURLConnection.HTTP_OK) {
BufferedReader br = new BufferedReader(
new InputStreamReader(con.getInputStream(), "utf-8"));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
br.close();
JSONObject jObj = new JSONObject(sb.toString());
result = jObj.toString();
if (result != null) {
return result;
}
} else {
System.out.println(con.getResponseMessage());
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
Log.e("result","null "+result);
}
}
}
UPDATE
デバッグした後、私は、CakePHPに問題があることが表示されます。私は、このエラーメッセージました:
Deprecated: Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead. in Unknownon line 0
Warning: Cannot modify header information - headers already sent in Unknown on line 0
Warning (512): Unable to emit headers. Headers sent in file= line=0 [CORE/src/Http/ResponseEmitter.php, line 48]
をしかし、それは郵便配達で動作する理由を私は知らないし、それはアンドロイドで作業していません!私はあなたがHTML
を得る理由です(サーバーは、いくつかのヘッダやポストマンが送信していることと同様のものを期待しているし、あなたが、その後、サーバはエラーページに答えるされていないと思い例外の<br
メッセージに基づいて
ありがとうございました。問題はサーバからのもので、アンドロイドからは私のアップデートを確認していないようです。 AsyncTaskの使用をやめて、なぜもっと説明できますか? –
まず、sb.toString()にhtlmまたはjsonが含まれているかどうかを確認する必要があります。そしてそれを盲目的にjsonオブジェクトに変換しないでください。そして、HttpUrlConnectionとAsynctaskの使用には何も問題ありません。 – greenapps
asynctaskについてオリエンテーションの変更に問題があることはかなり間違っており、メモリリークが発生する可能性がある理由を理解できないことを示しています。 Asynctaskもアクティビティライフサイクルに結合していません –