1

我想使用jersey API在 java 中创建一个安静的 web 服务并在 android 应用程序中使用它。我在 SO 上遇到了这个问题,但它谈论的是 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 应用程序中使用了此服务。

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

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。相反,它返回 status code = 500

任何帮助表示赞赏。

4

2 回答 2

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();
    }
于 2013-05-10T10:21:43.090 回答
1

最好查看服务器端日志以更好地理解。

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

StringEntity stringEntity = new StringEntity(myJsonDocStr, HTTP.UTF_8);
stringEntity.setContentType("application/json");
于 2013-05-10T14:29:18.067 回答