ChiniBottle
2016-03-13 15:57
采纳率: 100%
浏览 1.8k

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 params,Handler 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(nameValuePairs,HTTP.UTF_8));
            HttpResponse rsp = httpClient.execute(post);
            HttpEntity httpEntity = rsp.getEntity();
            displayString += EntityUtils.toString(httpEntity);
        }
        catch (Exception e)
        {
            //Toast.makeText(null,e.toString(), Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
        return displayString;
    }
}

发送带不带参数的Post请求服务器端都无法得到。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

7条回答 默认 最新

  • bdmh 2016-03-13 23:20
    已采纳

    如果发送错误,你应该能看到错误日志的,然后核查url是否正常,端口是否打开等网络问题

    点赞 打赏 评论
  • 找工作那些事 2016-03-14 00:09

    典型问题,没开子线程,网络操作要在工作线程执行

    点赞 打赏 评论
  • gincoo 2016-03-14 01:00
     new Thread(
                    public void run(){
    
    }).start();
    
    点赞 打赏 评论
  • 东风玖哥 2016-03-14 01:29

    初学者不要弄清楚原理,用OKHttp或者Volley就能解决问题

    点赞 打赏 评论
  • 屈楚 2016-03-14 02:16

    Android的4.0版本后,网络请求操作要在子线程里面运行。不能在主线程里面。

    点赞 打赏 评论
  • 侯位移 2016-03-14 08:42

    看url像内部地址,是否已经连上内网进行请求?

    点赞 打赏 评论
  • smileNicky 2016-03-15 03:12

    Android4.0的都要放在线程里,一般都是通过异步任务获取Handler机制的
    你可以参考我的博客哦!http://blog.csdn.net/u014427391/article/details/50255285
    希望可以帮助您!

    点赞 打赏 评论

相关推荐 更多相似问题