【发布时间】:2017-07-28 02:31:00
【问题描述】:
我正在尝试使用 cakephp api 将数据发布到服务器。使用邮递员我的 api 工作正常,但在 Android 上我遇到了一些问题。我为此使用了 asynctask,但我认为我可能对要发布的 json 有问题。 我收到一条错误消息告诉我
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);
}
}
}
更新
调试后,我可能对 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]
但我不知道为什么它适用于邮递员并且它不适用于android!
【问题讨论】: