Answer the question
In order to leave comments, you need to log in
How to get a response from the server in a POST request?
In general, my POST request is sent successfully, but how do I get a server response? In what format should you give an answer (just take the output from name.php, JSON or send everything in an xml file)?
Here is my code;
package ru.steapу.client;
import android.app.Activity;
import android.os.Bundle;
import android.os.AsyncTask;
import android.view.View;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.ProgressBar;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import android.widget.Toast;
public class LogActivity extends Activity
{
String SERVER_URL = "сам сайт";
EditText inputLogin, inputPass;
ProgressBar pb;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.log);
inputLogin = (EditText)findViewById(R.id.inputLogin);
inputPass = (EditText)findViewById(R.id.inputPass);
pb = (ProgressBar)findViewById(R.id.progressBar1);
pb.setVisibility(View.GONE);
}
public void buttonLog(View v){
if(inputLogin.getText().toString().length()<1){
Toast.makeText(LogActivity.this,"Введите ваш логин",Toast.LENGTH_LONG).show();
}
else if(inputPass.getText().toString().length()<1){
Toast.makeText(LogActivity.this,"Введите ваш пароль",Toast.LENGTH_LONG).show();
}else{
pb.setVisibility(View.VISIBLE);
new MyAsyncTask().execute(inputLogin.getText().toString(), inputPass.getText().toString());
}
}
private class MyAsyncTask extends AsyncTask<String, Integer, Double>{
@Override
protected Double doInBackground(String... params){
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(SERVER_URL);
try{
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("login", params[0]));
nameValuePairs.add(new BasicNameValuePair("pass", params[1]));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
HttpResponse response = httpclient.execute(httppost);
}catch(ClientProtocolException e){
Toast.makeText(getBaseContext(),"Client Protocol Exception",Toast.LENGTH_SHORT).show();
}catch(IOException e){
Toast.makeText(getBaseContext(),"IO Exception",Toast.LENGTH_SHORT).show();
}
return null;
}
protected void onPostExecute(Double result){
pb.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(),"Отправлено",Toast.LENGTH_LONG).show();
inputLogin.setText("");
inputPass.setText("");
}
protected void onProgressUpdate(Integer... progress){
pb.setProgress(progress[0]);
}
}
}
Answer the question
In order to leave comments, you need to log in
If you find it in a book, throw it away. This is a deprecated api.
On topic response.getEntity().getContent() will give you a response stream.
From my kindness:
Your activity is leaking through this asynctask (memory leak). Asynctasks are not for the network at all.
Take OkHttp, everything is easier and more beautiful there. If it's a Rest-like api, get Retrofit as well. Do not suffer.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question