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是否正常,端口是否打開等網絡問題