【发布时间】:2015-07-13 22:49:39
【问题描述】:
我正在将服务从普通域 DNS 服务器迁移到仅 IP 服务器,这为我的应用程序提供了 json 服务,问题是我无法在新 URL 中使用以下代码检索 JSONArray:
protected JSONArray doInBackground(String... arg0) {
String reponse;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse responce = httpclient.execute(httppost);
HttpEntity httpEntity = responce.getEntity();
reponse = EntityUtils.toString(httpEntity);
return new JSONArray(reponse);
//With the oldURL I printed the JSON as a string and everithing OK but the new URL returns a null string of JSON
}catch(Exception e){
e.printStackTrace();
}
return null;
}
String newurlexample = "http://111.22.333.44:1234/FOLD/blablabla";
String oldurl = "https:/example.com/FOLD/file.json";
我得到以下日志:
07-13 17:47:02.142: W/System.err(18824): org.json.JSONException: Value Method of type java.lang.String cannot be converted to JSONArray
07-13 17:47:02.145: W/System.err(18824): at org.json.JSON.typeMismatch(JSON.java:111)
07-13 17:47:02.145: W/System.err(18824): at org.json.JSONArray.<init>(JSONArray.java:96)
07-13 17:47:02.146: W/System.err(18824): at org.json.JSONArray.<init>(JSONArray.java:108)
07-13 17:47:02.146: W/System.err(18824): at com.karlol.***.Doctor_Fragment$GetData.doInBackground(Doctor_Fragment.java:171)
07-13 17:47:02.146: W/System.err(18824): at com.karlol.***.Doctor_Fragment$GetData.doInBackground(Doctor_Fragment.java:1)
07-13 17:47:02.147: W/System.err(18824): at android.os.AsyncTask$2.call(AsyncTask.java:288)
07-13 17:47:02.147: W/System.err(18824): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-13 17:47:02.147: W/System.err(18824): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
07-13 17:47:02.147: W/System.err(18824): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
【问题讨论】:
-
我的第一反应是:你为什么不用Volley?
-
确保您可以访问您的 IP URL 并返回有效的 JSON。还打印“reponse”变量以查看服务器返回的内容
-
URL 返回一个有效的 JSON 并且“reponse”返回:
Method[POST]Not Allowed, Please Make sure the REST MEthod [POST] for resource [my new address] is available in the other side! -
您得到的实际输出是什么?添加响应的打印输出。因为这听起来像是您将字符串强制转换为 json 数组...
-
你是把 JSON 放到服务器还是从服务器检索 file.json?如果是后者,我想你会想要一个 GET 操作。您的 Web 服务器告诉您它不知道或不允许在
blahblah位置进行 POST。无论哪种情况,请尝试将blahblah替换为file.json。
标签: android json httpclient