我需要將我的註冊表單詳細信息提交給服務器api。我已經嘗試了通過調用按鈕單擊函數的一種方法。但是沒有任何東西在服務器上發佈,我也沒有得到任何異常。請幫助我。 在此先感謝。 以下是代碼。將表單數據發佈到服務器Api(Android)
我已經調用的函數這樣
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try{
// CALL GetText method to make post method call
GetText();
}
catch(Exception ex)
{
ex.printStackTrace();;
Toast.makeText(getActivity(),"URL Exception", Toast.LENGTH_LONG).show();
}
和下面給出的是功能
public String GetText() throws UnsupportedEncodingException
{
HttpURLConnection connection = null;
// Get user defined values
Name = full_name.getText().toString();
Phonenumber = phone.getText().toString();
Email=email.getText().toString();
Password=password.getText().toString();
House = house.getText().toString();
Street = street.getText().toString();
Landmark = landmark.getText().toString();
// Create data variable for sent values to server
String data = URLEncoder.encode("name", "UTF-8")
+ "=" + URLEncoder.encode(Name, "UTF-8");
data += "&" + URLEncoder.encode("email", "UTF-8") + "="
+ URLEncoder.encode(Email, "UTF-8");
data += "&" + URLEncoder.encode("password", "UTF-8") + "="
+ URLEncoder.encode(Password, "UTF-8");
data += "&" + URLEncoder.encode("phone", "UTF-8")
+ "=" + URLEncoder.encode(Phonenumber, "UTF-8");
data += "&" + URLEncoder.encode("house", "UTF-8")
+ "=" + URLEncoder.encode(House, "UTF-8");
data += "&" + URLEncoder.encode("street", "UTF-8")
+ "=" + URLEncoder.encode(Street, "UTF-8");
data += "&" + URLEncoder.encode("areaname", "UTF-8")
+ "=" + URLEncoder.encode(Area, "UTF-8");
data += "&" + URLEncoder.encode("landmark", "UTF-8")
+ "=" + URLEncoder.encode(Landmark, "UTF-8");
// Send data
try
{
// Defined URL where to send data
URL url = new URL("http://application.easypani.com/app/customer/register");
// Send POST data request
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", "" +
Integer.toString(data.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
//send request
DataOutputStream wr = new DataOutputStream (
connection.getOutputStream());
wr.writeBytes(data);
wr.flush();
wr.close();
// Get the server response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
return response.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if(connection != null) {
connection.disconnect();
}
}
}
你應該使用'異步任務'來做到這一點。 – Paritosh
我不明白。您嘗試使用GET發送數據,但是您使用POST請求方法 –
您請幫助我這樣做。因爲我是android新手。 –