Web服务没有响应

时间:2011-09-27 05:21:24

标签: android web-services

我遇到一个问题,我正在调用一个在谷歌Chrome RESTClient AddON软件上运行的网络服务并提供200响应但是当我在我的Android应用程序中调用它时它没有响应。

我不知道Android有什么问题。请告诉我正确的结果。

由于

错误堆栈

09-27 10:41:14.582: INFO/System.out(373): <HTML><TITLE>404 Not Found</TITLE><BODY><H1>404 Not Found</H1><P>Unable to connect to host</BODY></HTML>
09-27 10:41:14.582: WARN/System.err(373): org.json.JSONException: Value <HTML><TITLE>404 of type java.lang.String cannot be converted to JSONObject
09-27 10:41:14.582: WARN/System.err(373):     at org.json.JSON.typeMismatch(JSON.java:107)
09-27 10:41:14.582: WARN/System.err(373):     at org.json.JSONObject.<init>(JSONObject.java:158)
09-27 10:41:14.582: WARN/System.err(373):     at org.json.JSONObject.<init>(JSONObject.java:171)
09-27 10:41:14.582: WARN/System.err(373):     at com.equinix.android.parsing.Parse_Json.parse_ShowOrders(Parse_Json.java:323)
09-27 10:41:14.582: WARN/System.err(373):     at com.equinix.android.showmyorders.ShowMyOrders$2.run(ShowMyOrders.java:112)

代码:

 try{
                               HttpPost post = new HttpPost("http://qa.mobile.equinix.com/eqixmobile/siteservice/order/getsitevisitsByCustOrgId");
                               StringEntity se = new StringEntity("{\"userKey\":\"68782\",\"custOrgId\":\"37\",\"credentials\":{\"password\":\"welcome1\",\"username\":\"mobileuser1\"},\"ibxCode\":\"SV1\",\"orderStatusList\":[\"SAVED\",\"SUBMITTED\"],\"pagination\":{\"pageNo\":1,\"pageSize\":6}}");
                               obj = new Service_Call(post, se, "mobileuser1", "welcome1",false);                                 
                               String json_string = obj.call_Service();
                               new Parse_Json(json_string).parse_ShowOrders();
                           }catch(Exception e)
                           {
                               e.printStackTrace();
                           }

致电服务方式:

package com.equinix.android.servicecall;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.json.JSONObject;

import com.equinix.android.common.MenuScreen;
import com.equinix.android.parsing.Parse_Json;

import com.equinix.android.parsing.Parse_Json;
import com.equinix.android.sitevisit.Site_Visit_Details;

public class Service_Call {

    HttpPost post;
    StringEntity eq_Credentials;
    String usr_Name,pass;
    boolean flag;
    public static int status_code=0;
    public Service_Call(HttpPost post,StringEntity eq_Credentials, String usr_Name, String pass,boolean flag)
    {
        this.post = post;
        this.eq_Credentials = eq_Credentials;
        this.usr_Name = usr_Name;
        this.pass = pass;
        this.flag = flag;
    }

    public String call_Service()
    {
          HttpClient client = new DefaultHttpClient();
          HttpConnectionParams.setConnectionTimeout(client.getParams(), 20000); //Timeout Limit
          HttpResponse response;

          try{
             // HttpPost post = new HttpPost("http://122.180.114.68/eqixmobile/siteservice/um/ibx");
           if(flag)
           {

              eq_Credentials.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
              post.setEntity(eq_Credentials);

              response = client.execute(post);
              System.out.println("The request body:"+post);
           }
           else
           {
               post.setHeader("Content-type", "application/json");
               post.setHeader("accept", "application/json");
               eq_Credentials.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));

               post.setEntity(eq_Credentials);

               response = client.execute(post);
               System.out.println("The request body:"+post.toString());
               status_code = response.getStatusLine().getStatusCode();
               System.out.println("The Response Code:"+response.getStatusLine().getStatusCode());
           }



                  BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
                  StringBuilder builder = new StringBuilder();
                  String str=""; 

                  String line = null; 
                   while ((line = reader.readLine()) != null) { 
                      str +=line; 

                   }


                 System.out.println(str);


                 return str;
          }
          catch(Exception e){
              e.printStackTrace();

          }
       return null;
    }
}

1 个答案:

答案 0 :(得分:0)

请从代码中删除以下行。

* eq_Credentials.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,“application / json”)); *

希望这对你有所帮助。