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

标签 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);
        }
    }
}

我刚开始使用其余 Web 服务。我成功创建了一个示例休息服务,它使用字符串并返回字符串,并在 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。相反,它返回状态代码 = 500

感谢任何帮助。

最佳答案

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

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

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

关于java - IOException:尝试使用休息服务时出现内部服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16479737/

相关文章:

android - 如何自动将缺少的 xml 属性添加到 android studio 中的特定 View

c# - 在 .NET ASMX 网络服务中获取 session

java - 使用 PICK_CONTACT 运行第二种情况

android - 没有足够的空间来展示广告

java - 设置背景颜色 : Android

java - 无法解析 MY_PERMISSION_ACCESS_COURSE_LOCATION

python - 如何从 Azure ML 中的 python 脚本获取 Web 服务的输出

json - 将 pouchDB 与 json 数据同步

java - 如何修复 '' 创建名称在类路径资源中定义的 bean 时出错”?

java - 如何修复 Java ArrayList 中循环中仅重复的最后一个对象