Java 在android中调用webservice时发生异常?

Java 在android中调用webservice时发生异常?,java,android,json,web-services,rest,Java,Android,Json,Web Services,Rest,从android应用程序调用webservice时,我有两到三个执行选项。当我从2.3.3(Emulator)版本的应用程序调用webservice时,我得到了异常,比如4.2.1(real device)版本的UnostException、connectiontimeoutexception和3.1版本的正常工作,我不知道为什么会发生这种情况。我试图从昨天开始解决这个异常,但还没有解决,如果代码中需要任何更改,请建议我 在LoginActivity中,我调用发出http请求的方法 List&l

从android应用程序调用webservice时,我有两到三个执行选项。当我从2.3.3(Emulator)版本的应用程序调用webservice时,我得到了异常,比如4.2.1(real device)版本的UnostException、connectiontimeoutexception和3.1版本的正常工作,我不知道为什么会发生这种情况。我试图从昨天开始解决这个异常,但还没有解决,如果代码中需要任何更改,请建议我

LoginActivity中,我调用发出http请求的方法

List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("tag", login_tag));
    params.add(new BasicNameValuePair("email", username));
    params.add(new BasicNameValuePair("password", userpsw));
    JsonParserWebs jsonDataFromSrvr = new JsonParserWebs();
    String loginData = jsonDataFromSrvr.makeHttpReqToSrvr(loginUrl,"POST", params);
List params=new ArrayList();
添加(新的BasicNameValuePair(“标签”,登录标签));
添加(新的BasicNameValuePair(“电子邮件”,用户名));
添加(新的BasicNameValuePair(“密码”,userpsw));
JsonParserWebs jsonDataFromSrvr=新JsonParserWebs();
字符串loginda=jsonDataFromSrvr.makeHttpReqToSrvr(loginUrl,“POST”,参数);
下面是调用webservice的JsonParserWebs

public String makeHttpReqToSrvr(String url,String requestType,List<NameValuePair> params) {
Log.i(JsonParserWebs.class.getName(),"URL..."+url);
HttpEntity httpEntity=null;

//making http request
try {

    if (requestType == "GET") {

        //connection time out
        HttpParams httpParameters = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
        HttpConnectionParams.setSoTimeout(httpParameters, 10000);

        HttpClient httpClient = new DefaultHttpClient(httpParameters);
        String paramString =URLEncodedUtils.format(params, "utf-8");
        HttpGet httpGet = new HttpGet(url+"?"+paramString);



        HttpResponse httpResp = httpClient.execute(httpGet);
        httpEntity = httpResp.getEntity();

    }
    if (requestType == "POST") {

        //connection time out
        // From stackoverflow, I addes following three line but still got ConnectTimeoutException
        HttpParams httpParameters = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
        HttpConnectionParams.setSoTimeout(httpParameters, 10000);

        HttpClient  httpClient = new DefaultHttpClient(httpParameters);
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new UrlEncodedFormEntity(params));

        HttpResponse httpResp = httpClient.execute(httpPost);
        httpEntity = httpResp.getEntity();

    }

} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

try {

    json = EntityUtils.toString(httpEntity);
    Log.v("JSON", "data"+json);
} catch (Exception e) {
    e.printStackTrace();
} 
// try parse the string to a JSON object

return json;
公共字符串makeHttpRequestOSRVR(字符串url、字符串请求类型、列表参数){
Log.i(JsonParserWebs.class.getName(),“URL…”+URL);
HttpEntity HttpEntity=null;
//发出http请求
试一试{
if(requestType==“GET”){
//连接超时
HttpParams httpParameters=新的BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters,5000);
HttpConnectionParams.setSoTimeout(httpParameters,10000);
HttpClient HttpClient=新的默认HttpClient(httpParameters);
String paramString=URLEncodedUtils.format(params,“utf-8”);
HttpGet HttpGet=新的HttpGet(url+“?”+参数字符串);
HttpResponse httpResp=httpClient.execute(httpGet);
httpEntity=httpResp.getEntity();
}
if(requestType==“POST”){
//连接超时
//在stackoverflow中,我添加了下面三行,但仍然得到了ConnectTimeoutException
HttpParams httpParameters=新的BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters,5000);
HttpConnectionParams.setSoTimeout(httpParameters,10000);
HttpClient HttpClient=新的默认HttpClient(httpParameters);
HttpPost HttpPost=新的HttpPost(url);
setEntity(新的UrlEncodedFormEntity(参数));
HttpResponse httpResp=httpClient.execute(httpPost);
httpEntity=httpResp.getEntity();
}
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
json=EntityUtils.toString(httpEntity);
Log.v(“JSON”、“数据”+JSON);
}捕获(例外e){
e、 printStackTrace();
} 
//尝试将字符串解析为JSON对象
返回json;
}


提前感谢

您需要使用AsyncTask,否则它将崩溃

1.使用AsyncTask当您使用耗时的过程时,从其他方面来看,您将遇到网络异常
2.在.mainfeat中获取internet权限

请发布堆栈跟踪。请发布崩溃的logcat输出(这几乎肯定是由于未捕获的异常)。在Eclipse中,通过打开logcat视图可以看到logcat的输出。