问题描述
- Android关于HttpPost无法发送的问题
- Manifest里添加了Internet权限
Activity类
import com.google.gson.Gson;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.os.Handler;import android.os.Message;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 ActionBarActivity { Button sendButton; EditText userEditText pwdEditText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sendButton = (Button) findViewById(R.id.button1); userEditText = (EditText)findViewById(R.id.editText1); pwdEditText = (EditText)findViewById(R.id.editText2); //利用Handler更新UI final Handler h = new Handler(){ @Override public void handleMessage(Message msg) { Toast.makeText(MainActivity.this msg.toString() Toast.LENGTH_LONG).show(); } }; sendButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { User user = new User(); user.setName(userEditText.getText().toString().trim()); user.setPwd(pwdEditText.getText().toString().trim()); Gson gson = new Gson(); String userJasonString = gson.toJson(user); new Thread(new AccessNetwork(""192.168.199.1:8888/gcgl/user.do?method=login""userJasonString h)).start(); } }); }}
创建的线程AccessNetwork
import java.util.ArrayList;import java.util.List;import org.apache.http.HttpEntity;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.impl.client.DefaultHttpClient;import org.apache.http.message.BasicNameValuePair;import org.apache.http.params.CoreConnectionPNames;import org.apache.http.protocol.HTTP;import org.apache.http.util.EntityUtils;import android.os.Handler;import android.os.Message;import android.util.Log;import android.widget.Toast;public class AccessNetwork implements Runnable{ private String url; private String params; private Handler h; public AccessNetwork(String url String paramsHandler h) { super(); this.url = url; this.params = params; this.h = h; } @Override public void run() { Message m = new Message(); m.obj = sendPost(url params); h.sendMessage(m); } public static String sendPost(String url String params) { List<NameValuePair> nameValuePairs; String displayString=""1""; try { HttpClient httpClient = new DefaultHttpClient(); HttpPost post = new HttpPost(url);// httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT 15000); // httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT 15000);// nameValuePairs= new ArrayList<NameValuePair>();// nameValuePairs.add(new BasicNameValuePair(""params"" params));// post.setEntity(new UrlEncodedFormEntity(nameValuePairsHTTP.UTF_8)); HttpResponse rsp = httpClient.execute(post); HttpEntity httpEntity = rsp.getEntity(); displayString += EntityUtils.toString(httpEntity); } catch (Exception e) { //Toast.makeText(nulle.toString() Toast.LENGTH_LONG).show(); e.printStackTrace(); } return displayString; }}
发送带不带参数的Post请求服务器端都无法得到。
解决方案
如果发送错误,你应该能看到错误日志的,然后核查url是否正常,端口是否打开等网络问题
解决方案二:
典型问题,没开子线程,网络操作要在工作线程执行
解决方案三:
new Thread( public void run(){}).start();
解决方案四:
初学者不要弄清楚原理,用OKHttp或者Volley就能解决问题
解决方案五:
Android的4.0版本后,网络请求操作要在子线程里面运行。不能在主线程里面。
解决方案六:
看url像内部地址,是否已经连上内网进行请求?
解决方案七:
Android4.0的都要放在线程里,一般都是通过异步任务获取Handler机制的
你可以参考我的博客哦!http://blog.csdn.net/u014427391/article/details/50255285
希望可以帮助您!
时间: 2024-11-05 06:04:16