关于android HttpClient发送post的问题
问题是这样,现在需要用手机给php网站发送数据,可是那边收不到,本地抓包也没内容。代码是照书上改的,manifest里面权限也给了,怎么会发不出去呢,求助。代码如下package com.example.http_model;--------------------编程问答-------------------- 发送http请求不能写在activity的主线程里,需要另外开个线程来实现这个功能 --------------------编程问答-------------------- 好像是4.0以上开始的不能在主线程里进行网络访问。
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.content.Entity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
Button btn;
EditText username;
EditText x;
EditText y;
HttpClient httpClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.button1);
username=(EditText)findViewById(R.id.editText1);
x=(EditText)findViewById(R.id.editText2);
y=(EditText)findViewById(R.id.editText3);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
username.setText("clicked");
String str1=username.getText().toString();
String str2=x.getText().toString();
String str3=y.getText().toString();
HttpPost post=new HttpPost("http://carebaby.duapp.com/system/receivemessage.php");
List<NameValuePair>params=new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", str1));
params.add(new BasicNameValuePair("y", str2));
params.add(new BasicNameValuePair("x", str3));
try
{
post.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
HttpResponse response=httpClient.execute(post);
//if(response.getStatusLine().getStatusCode()==200)
//{
String msg=EntityUtils.toString(response.getEntity());
Toast.makeText(MainActivity.this, msg, 5000).show();
//}
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
另外,这个肯定会报异常的,你没看么? --------------------编程问答-------------------- 本来需要重新启动一个activity,不过你可以在
super.onCreate(savedInstanceState);后面加入以下代码,就可以在一个activty访问网络了。
setContentView(R.layout.activity_main);
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()--------------------编程问答-------------------- 楼主,这代码看起来没什么问题 --------------------编程问答--------------------
.detectDiskReads()
.detectDiskWrites()
.detectNetwork() // or .detectAll() for all detectable problems
.penaltyLog()
.build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects()
.penaltyLog()
.penaltyDeath()
.build());
同意1楼的 --------------------编程问答--------------------
public class MainActivity extends Activity {
private Thread mThread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mThread = new Thread(runnable);
mThread.start();//线程启动
}
Runnable runnable = new Runnable() {
@Override
public void run() {//run()在新的线程中运行
HttpClient hc = new DefaultHttpClient();
HttpPost post=new HttpPost("http://carebaby.duapp.com/system/receivemessage.php");
List<NameValuePair>params=new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", "username"));
params.add(new BasicNameValuePair("y", "x"));
params.add(new BasicNameValuePair("x", "y"));
try {
HttpResponse hr = hc.execute(post);
Toast.makeText(getApplicationContext(), hr.getEntity().getContent().toString(),
Toast.LENGTH_SHORT).show();
}
catch(Exception ex){
Log.i("http", "1"+ex.getMessage());
}
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
像这样开一个线程怎么还是不行呐, --------------------编程问答-------------------- 能运行,但是网络没用啊。。。。 --------------------编程问答-------------------- 辛苦大大看一下6楼,我开了一个线程还是不行
补充:移动开发 , Android