1

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

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 失败,我的应用程序崩溃,我遇到了问题:它是带有 LoginRequest 的 HttpRequest,它是我创建的自定义类,如果我删除这 2 行代码,活动就会开始......

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>
4

1 回答 1

0

在将您的 HTTP 请求放在首位之前,您需要使用连接管理器检查您是否与其中任何一个连接

无线上网

或者

移动数据检查这个第一次在清单文件中添加以下权限

<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 请求有一个标准,它应该有一个像 wifi 或移动网络这样的驱动程序,用你的信息把他带到外面的世界希望这会对你有所帮助

于 2013-01-20T11:18:58.547 回答