ANDROID:HTTP请求后启动活动时应用程序崩溃

时间:2013-01-20 10:04:31

标签: android android-activity crash httprequest

我有一个问题需要快速解决:) 我有这段代码:

loginBtn.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            HttpRequest hr = new HttpRequest();
            String response = hr.LoginRequest(emailText.getText().toString(), pwdText.getText().toString());
            res.setText(response);
            res.setVisibility(0);

            if(response.equals("pr_code_0101")) {
                Login.this.startActivity(bcard_intent);
            }
        }
    });

startActivity失败,我的应用程序崩溃了,我遇到了什么问题:这是HttpRequest,LoginRequest是我创建的自定义类,如果我删除活动开始的那两行代码......

Http的代码

public String LoginRequest(String email, String password)
{
    List<NameValuePair> nvp = new ArrayList<NameValuePair>(3);
    nvp.add(new BasicNameValuePair("email", email));
    nvp.add(new BasicNameValuePair("password", password));
    nvp.add(new BasicNameValuePair("hash_app", hash_code));

    return Set("******/loginrequest.php", nvp);
}

public String Set(String url,List<NameValuePair> list)
{
     StringBuilder builder = new StringBuilder();
     HttpClient client = new DefaultHttpClient();
     HttpPost httpPost = new HttpPost(url);

     try {
        httpPost.setEntity(new UrlEncodedFormEntity(list));
        HttpResponse response = client.execute(httpPost);

        HttpEntity entity = response.getEntity();
        InputStream content = entity.getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(content));
        String line;
        while ((line = reader.readLine()) != null)
        {
           builder.append(line);
        } 

        reader.close();
        content.close();

     }
     catch (ClientProtocolException e) {
         return e.toString();
     }
     catch (IOException e) {
         return e.toString();
    }

     return builder.toString();
}

我该如何解决我的问题?

这里是logcat:

 01-20 11:25:42.824: E/AndroidRuntime(274): java.lang.RuntimeException: Unable to start activity ComponentInfo{name.printernet.app/name.printernet.app.BCard}: android.view.InflateException: Binary XML file line #7: Error inflating class <unknown>

1 个答案:

答案 0 :(得分:0)

在首先放置HTTP请求之前,您可以使用连接管理器检查是否与

连接

wifi

要检查此第一个的移动数据,请在清单文件中添加以下权限

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

然后在你班上检查可用性

    ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

//mobile
State mobile = conMan.getNetworkInfo(0).getState();

//wifi
State wifi = conMan.getNetworkInfo(1).getState();

然后像这样使用它:

if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING) {
    //mobile
} else if (wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING) {
    //wifi
}

之后发送HTTP请求 因为HTTP请求有一个critera,它应该有一个像wifi或移动网络的驱动程序带他到外面的世界,你的信息希望这将有助于你