我无法从我的 Android 应用程序向服务器发送发布请求。我找到了一些关于如何发送POST的示例,但我的代码有一些错误,代码如下:
public class MainActivity extends Activity
{
private WebView wv; //Internet
private EditText email1; //Edit's
private EditText email2; //Edit's
private Button btn_get_access; //Get Access
private String post_url = "http://rasnacis.lv/vova.php";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wv = (WebView) findViewById(R.id.webView1);
email1 = (EditText) findViewById(R.id.txt_email_1);
email2 = (EditText) findViewById(R.id.txt_email_2);
btn_get_access = (Button) findViewById(R.id.btn_get_access);
WebSettings webSettings = wv.getSettings();
webSettings.setSaveFormData(true);
//BUTTON
OnClickListener ocl_btn_get_access = new OnClickListener()
{
public void onClick(View v)
{
String givenEmail1 = email1.getEditableText().toString();
String givenEmail2 = email2.getEditableText().toString();
//SENDING POST
if (givenEmail1.length() > 0 && givenEmail2.length() > 0)
{
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(post_url);
try
{
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("email1", "email2"));
nameValuePairs.add(new BasicNameValuePair("email1", "slgjlskjgsg"));
nameValuePairs.add(new BasicNameValuePair("email2", "xkjfhgkdjfhgkdjfg"));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpClient.execute(httpPost);
}
catch (ClientProtocolException e)
{
System.out.println("First Exception caz of HttpResponese :" + e);
e.printStackTrace();
}
catch (IOException e)
{
System.out.println("Second Exception caz of HttpResponse :" + e);
e.printStackTrace();
}
}
else
{
Toast.makeText(getBaseContext(), "All fields are required!", Toast.LENGTH_SHORT).show();
}
//sending GET
//wv.loadUrl("http://rasnacis.lv/vova.php?email1=" + email1.getText() + "&email2=" + email2.getText());
}
};
btn_get_access.setOnClickListener(ocl_btn_get_access);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
那么有人可以帮我吗?我刚开始Android开发,不知道很多技巧或困难的东西......