我想在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:85是throw new IOException(statusLine.getReasonPhrase());
这意味着statusLine.getStatusCode()
没有返回HttpStatus.SC_OK
。相反,它返回状态代码= 。
任何帮助表示赞赏。
首先,尝试服务器日志设置的内容类型,得到异常。服务器是否可靠或间歇地失效?您是否使用非Android实现(java/wget/curl)重现错误?你见过通过网络传输的HTTP数据吗? (服务器上的tcpdump -Xs1600可能会有所帮助) – 2013-05-10 10:12:23
statusLine.getStatusCode()不返回HttpStatus.SC_OK – GAMA 2013-05-10 10:31:19
也尝试过用简单的java客户端。同样的错误。 – GAMA 2013-05-10 10:38:49