无法从IP服务器检索JSONArray,但我可以从普通服务器?

Kar*_*pez 6 android json httpclient

我将服务从普通域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";
Run Code Online (Sandbox Code Playgroud)

我得到以下日志:

    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)
Run Code Online (Sandbox Code Playgroud)

Moh*_*iri 2

从你的问题的标题我可以注意到你使用的URL和新的IP之间有问题..首先你需要确保你的新网络服务提供与旧的相同的结果..无论如何你可以尝试使用以下方法检索您的数据:

try{
       HttpClient httpclient = new DefaultHttpClient();
       HttpPost httppost = new HttpPost(url);
       HttpResponse responce = httpclient.execute(httppost);
       HttpEntity httpEntity = responce.getEntity();
       is = httpEntity.getContent();
       BufferedReader reader = new BufferedReader(new InputStreamReader(
            is));

      StringBuilder sb = new StringBuilder();
      String line = null;
      while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
        reponse = sb.toString();


       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();

}
Run Code Online (Sandbox Code Playgroud)

编辑:

如果您使用共享托管服务器.. 在同一 IP 上拥有多个网站并不罕见,仅通过站点名称进行区分(所谓的虚拟托管)。您所做的仅适用于给定 IP 上只有一个站点的情况。

编辑2

您需要使用 RESTful 插件(chrome 或 firefox)测试您的网络服务,即高级休息客户端