2013-05-10 96 views
1

我想在java中使用球衣API创建一个restful web服务,并在android应用程序中使用它。我在SO上得到了this question,但它谈论了Java客户端,而我有Android客户端。IOException:尝试使用rest服务时发生内部服务器错误

我的服务是这样的:

@Path("/no") 
public class CheckNumber { 

@POST 
@Produces("application/json") 
@Consumes("application/json") 
public String getDetails(@PathParam("cNo") String cNo) { 
    String CardNo=""; 
    try { 
     JSONObject jsonObj = new JSONObject(cNo); 
     CardNo=jsonObj.getString("CardNo"); 
    } catch (ParseException e1) { 
     e1.printStackTrace(); 
    } 
    //Do something 
    return "someValue"; 
    } 
} 

现在到客户端:

public class MainActivity extends Activity { 

    JSONObject json = new JSONObject(); 
    String wsdl = "http://192.168.1.105:8080/restdemo/check/no/"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     new RequestTask().execute("1234567890"); 

    } 

    class RequestTask extends AsyncTask<String, String, String> { 

     @Override 
     protected String doInBackground(String... uri) { 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpResponse response; 
     String add = "{\"CardNo\":\"" + uri[0] + "\"}"; 
     HttpPost postMethod = new HttpPost(wsdl); 
     String responseString = null; 
     try { 
      postMethod.addHeader("Content-Type", "application/json"); 
      HttpEntity entity = new StringEntity(add); 
      postMethod.setEntity(entity); 
      response = httpclient.execute(postMethod); 
       StatusLine statusLine = response.getStatusLine(); 
       if (statusLine.getStatusCode() == HttpStatus.SC_OK) { 
        ByteArrayOutputStream out = new        ByteArrayOutputStream(); 
        response.getEntity().writeTo(out); 
        out.close(); 
        responseString = out.toString(); 
       } else { 
        response.getEntity().getContent().close(); 
        throw new IOException(statusLine.getReasonPhrase()); 
       } 
      } catch (ClientProtocolException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      return responseString; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      super.onPostExecute(result); 
     } 
    } 
} 

我刚开始使用REST Web服务。我成功创建了一个示例休息服务,它会消耗字符串并返回字符串,并在Android应用程序中使用此服务。

但是当我尝试使用POST方法传递json字符串。它给以下errorin日志:

java.io.IOException: Internal Server Error 
at com.example.restclient.MainActivity$RequestTask.doInBackground(MainActivity.java:85) 

其中MainActivity.java:85throw new IOException(statusLine.getReasonPhrase());这意味着statusLine.getStatusCode()没有返回HttpStatus.SC_OK。相反,它返回状态代码= 。

任何帮助表示赞赏。

+0

首先,尝试服务器日志设置的内容类型,得到异常。服务器是否可靠或间歇地失效?您是否使用非Android实现(java/wget/curl)重现错误?你见过通过网络传输的HTTP数据吗? (服务器上的tcpdump -Xs1600可能会有所帮助) – 2013-05-10 10:12:23

+0

statusLine.getStatusCode()不返回HttpStatus.SC_OK – GAMA 2013-05-10 10:31:19

+0

也尝试过用简单的java客户端。同样的错误。 – GAMA 2013-05-10 10:38:49

回答

1

这将是很好看的服务器端日志更好地理解。

尝试建立与UTF8的实体,并在string实体,而不是在的PostMethod

StringEntity stringEntity = new StringEntity(myJsonDocStr, HTTP.UTF_8); 
stringEntity.setContentType("application/json"); 
+0

我用'HttpPost postMethod = new HttpPost(“some url”) \t \t \t \t postMethod.addHeader(“Content-Type”,“application/json”); \t \t \t \t HttpEntity entity = new StringEntity(“some String”); \t \t \t \t postMethod.setEntity(entity);' – GAMA 2013-05-16 15:30:26

1

试试这个代码,它为我

Boolean NetworkLostFlag = false;  

HttpParams httpParameters = new BasicHttpParams(); 

    int timeoutConnection = 10000; 

    HttpConnectionParams.setConnectionTimeout(httpParameters, 
      timeoutConnection); 

    int timeoutSocket = 12000; 

    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); 
    HttpClient httpclient = new DefaultHttpClient(httpParameters); 
    HttpPost httppost = new HttpPost(strUrl"); 
      try { 
     httppost.setEntity(new UrlEncodedFormEntity(new BasicNameValuePair(arg1, val1), "UTF-8")); 
     HttpResponse response = httpclient.execute(httppost); 

     HttpEntity entity = response.getEntity(); 

     if (entity != null) { 
      InputStream instream = entity.getContent(); 
      try { 
       // do something useful 

       StringBuffer buffer = new StringBuffer(); 
       byte[] b = new byte[4096]; 
       for (int n; (n = instream.read(b)) != -1;) { 
        buffer.append(new String(b, 0, n)); 
       } 
       result = buffer.toString(); 

      } catch (Exception e) { 
       NetworkLostFlag = true; 
       // TODO: handle exception 
      } finally { 
       instream.close(); 
      } 
     } 
    } catch (Exception e) {   
     NetworkLostFlag = true; 
     e.printStackTrace(); 
    } 
+0

但你正在使用'httppost.setEntity(new UrlEncodedFormEntity(new BasicNameValuePair(arg1,val1),“UTF-8”));'而我想使用json字符串。 – GAMA 2013-05-10 10:27:18

相关问题