程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> 互聯網-Android關於HttpPost無法發送的問題

互聯網-Android關於HttpPost無法發送的問題

編輯:編程解疑
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請求服務器端都無法得到。

最佳回答:


如果發送錯誤,你應該能看到錯誤日志的,然後核查url是否正常,端口是否打開等網絡問題

ChiniBottle
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved