IOException:尝试使用休息服务时出现内部服务器错误

时间:2013-05-10 10:01:52

标签: java android web-services rest ioexception

我想使用 jersey API 在java中创建一个restful web服务,并在android应用程序中使用它。我在SO上得到了this question但它谈到了Java客户端,而我有android客户端。

我的服务如下:

@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);
        }
    }
}

我刚开始使用其他网络服务。我成功创建了一个示例休息服务,它使用字符串并返回字符串并在android app中使用此服务。

但是当我尝试使用POST方法传递json字符串时。它在日志中给出以下错误:

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。相反,它返回状态代码= 500

任何帮助表示感谢。

2 个答案:

答案 0 :(得分: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();
    }

答案 1 :(得分:1)

很高兴看到服务器端日志能够更好地理解。

尝试使用UTF8创建实体并在字符串实体中而不是在postMethod中设置内容类型

StringEntity stringEntity = new StringEntity(myJsonDocStr, HTTP.UTF_8);
stringEntity.setContentType("application/json");
相关问题