gpt4 book ai didi

java - Android Java Http 请求发布

转载 作者:可可西里 更新时间:2023-11-01 16:33:08 26 4
gpt4 key购买 nike

我创建了一个最简单的应用程序,一个默认应用程序,我想尝试从 php 页面发布和检索某些内容 MyActivty.Java 看起来像:

package com.dolganiuc.victor.myapplication;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

public class MainActivity extends ActionBarActivity {

private static final String TAG = "victorMessage";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "onCreate");
postData();


}

public void postData() {

// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://93.117.158.187/");

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

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

Log.i(TAG, response.toString());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}

}

}

当 Android Studio 调试一切正常时,我在 2 个设备 Android 4.4.2 KitKat 和 Android 5.0 Lollipop 上运行这些应用程序,并且在这两个设备上应用程序正在关闭检索错误不幸的是我的应用程序已关闭。

问题是什么?

系统错误

06-11 13:18:02.042    1142-1161/? W/System.err﹕ java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
06-11 13:18:02.042 1142-1161/? W/System.err﹕ at com.android.internal.telephony.PhoneBase.privatizeCellInfoList(PhoneBase.java:1252)
06-11 13:18:02.042 1142-1161/? W/System.err﹕ at com.android.internal.telephony.PhoneBase.getAllCellInfo(PhoneBase.java:1240)
06-11 13:18:02.042 1142-1161/? W/System.err﹕ at com.android.internal.telephony.PhoneProxy.getAllCellInfo(PhoneProxy.java:337)
06-11 13:18:02.042 1142-1161/? W/System.err﹕ at com.android.phone.PhoneInterfaceManager.getAllCellInfo(PhoneInterfaceManager.java:1486)
06-11 13:18:02.042 1142-1161/? W/System.err﹕ at com.android.internal.telephony.ITelephony$Stub.onTransact(ITelephony.java:691)
06-11 13:18:02.042 1142-1161/? W/System.err﹕ at android.os.Binder.execTransact(Binder.java:446)

最佳答案

第一个 问题可能是您没有添加 INTERNET在您的 AndroidManifest.xml 中获得许可.要解决此问题,只需添加此行 <uses-permission android:name="android.permission.INTERNET" />在你的 list 中。

其他 问题是您试图在主线程上执行发布请求。它导致NetworkOnMainThreadException .你应该使用 AsyncTaskHandler执行网络请求,像这样:

public class MainActivity extends ActionBarActivity {

private static final String TAG = "victorMessage";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "onCreate");
new HttpPostTask().execute();
}

private class HttpPostTask extends AsyncTask<Void, Void, String> {

@Override
protected String doInBackground(Void... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://93.117.158.187/");

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

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

Log.i(TAG, response.toString());
return response.toString();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}

return null;
}

@Override
protected void onPostExecute(String response) {
super.onPostExecute(response);

//do something with you response, connected with UI thread.
}
}
}

关于java - Android Java Http 请求发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30777927/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com