简体   繁体   English

进行api调用和android应用时出错

[英]Getting error while doing api call and android app is crash

i am new to android, im trying to api call with php , i have done one example for it, that work on another computer fine but same app package gives error in eclipse on my laptop. 我是Android的新手,我试图用php进行api调用,我为此做了一个示例,可以在另一台计算机上正常工作,但相同的应用程序包在我的笔记本电脑上出现了eclipse错误。

public void postData() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("id", "12345"));
        nameValuePairs.add(new BasicNameValuePair("stringdata", "Hi"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
} ` 

hear is my error log, i got this much of errors. 听到的是我的错误日志,我收到了很多错误。

//Try following way    
//List<NameValuePair> nameValuePairs 
HttpClient hc = new DefaultHttpClient();
HttpPost hp = new HttpPost(url);
UrlEncodedFormEntity obj=new UrlEncodedFormEntity(nameValuePairs);
obj.setChunked(true);
hp.setEntity(obj);
HttpResponse hr = hc.execute(hp);   
HttpEntity he = hr.getEntity(); 

Logcat is clear: Could not find a method postData(View) in the activity class . Logcat很清楚: Could not find a method postData(View) in the activity class I guess you've used xml layout for setting button action. 我想您已经使用xml布局来设置按钮操作。

In this case, just change your 在这种情况下,只需更改您的

public void postData() {
}

to

public void postData(View view) {
}

Note: setting button listeners this way isn't efficient. 注意:以这种方式设置按钮侦听器效率不高。

Also, you should perform network operations on another thread - using network on main thread will raise NetworkOnMainThreadException on Android 3.0+ 另外,您应该在另一个线程上执行网络操作-在主线程上使用网络会在Android 3.0+上引发NetworkOnMainThreadException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM