Java httppost导致应用程序崩溃
我正试图通过android studio中的java向我的PHP页面发送post请求 首先,我尝试了httppost,按下该键后应用程序崩溃 然后我读到,我必须使用新的线程,我这样做了,但仍然 应用程序崩溃了 代码如下:Java httppost导致应用程序崩溃,java,android,android-studio,Java,Android,Android Studio,我正试图通过android studio中的java向我的PHP页面发送post请求 首先,我尝试了httppost,按下该键后应用程序崩溃 然后我读到,我必须使用新的线程,我这样做了,但仍然 应用程序崩溃了 代码如下: public void send () { final String name,phone,fb; EditText one,two,three; one=(EditText) findViewById(R.id.editText); two=
public void send () {
final String name,phone,fb;
EditText one,two,three;
one=(EditText) findViewById(R.id.editText);
two=(EditText) findViewById(R.id.editText2);
three=(EditText) findViewById(R.id.editText3);
name=one.getText().toString();
phone=two.getText().toString();
fb=three.getText().toString();
Thread T=new Thread(new Runnable() {
@Override
public void run() {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://test.com/test.php");
try {
List <NameValuePair> nameValuePairs= new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("name",name));
nameValuePairs.add(new BasicNameValuePair("number",phone));
nameValuePairs.add(new BasicNameValuePair("fbname",fb));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response= httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
});
T.start();
}
公共作废发送(){
最终字符串名称、电话、fb;
编辑文本一、二、三;
一=(EditText)findViewById(R.id.EditText);
二=(EditText)findViewById(R.id.editText2);
三=(EditText)findViewById(R.id.editText3);
name=one.getText().toString();
phone=two.getText().toString();
fb=three.getText().toString();
线程T=新线程(新的可运行线程(){
@凌驾
公开募捐{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://test.com/test.php");
试一试{
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“name”,name));
添加(新的BasicNameValuePair(“电话号码”);
添加(新的BasicNameValuePair(“fbname”,fb));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
}捕获(IOE异常){
//TODO自动生成的捕捉块
}
}
});
T.开始();
}
日志:确保您在清单文件中设置了与internet通信的权限。如果没有,它将抛出一个
SecurityException
,并破坏你的应用程序
<uses-permission android:name="android.permission.INTERNET" />
@Rami Add
e.printStackTrace()
到您的捕获块并更新您的日志跟踪。@Rami我确信我有权限:(Manifest似乎很好,其余的活动如何?您在哪里调用send()
?让我们来看看。
public void send (View v) {