Here is the Server class within the server connection. I need to get the response and check the result after the server process complete. The following codes only check the result before got the respond.
public class Server {
String line;
StringBuilder stringBuilder = new StringBuilder();
public void httpPost(String URL) {
new Thread(new Runnable() {
#Override
public void run() {
try {
URL url = new URL(URL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.connect();
int responseCode = connection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK){
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
stringBuilder.setLength(0);
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
}
button.setOnClickListener(new View.OnClickListener() {
#RequiresApi(api = Build.VERSION_CODES.O)
#Override
public void onClick(View view) {
server.httpPost("https://google.com?username=Peter");
if (server.stringBuilder.toString().contains("Successful")){
Log.d("test",server.stringBuilder.toString());
}
}
}
});
Why you don't use retrofit library? Retrofit easy way to work with api's.
You can find here retrofit library
Retrofit has two callback:
call.enqueue(callback) - asynchronous, runs the request on a background thread, and runs the callback on the current thread
call.execute() - synchronous, runs the request on the current thread.
I use call.enqueue(callback). Because this is asynchronous and this callback does not freeze ui components.
You need learn REST API libraries volley, retrofit or ktor.
You can use any library.
Volley: https://google.github.io/volley/
Retrofit: https://square.github.io/retrofit/
Ktor: https://ktor.io/
Related
I have searched everywhere but I couldn't find my answer, is there a way to make a simple HTTP request? I want to request a PHP page / script on one of my websites but I don't want to show the webpage.
If possible I even want to do it in the background (in a BroadcastReceiver)
UPDATE
This is a very old answer. I definitely won't recommend Apache's client anymore. Instead use either:
Retrofit
OkHttp
Volley
HttpUrlConnection
Original Answer
First of all, request a permission to access network, add following to your manifest:
<uses-permission android:name="android.permission.INTERNET" />
Then the easiest way is to use Apache http client bundled with Android:
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpGet(URL));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
String responseString = out.toString();
out.close();
//..more logic
} else{
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
If you want it to run on separate thread I'd recommend extending AsyncTask:
class RequestTask extends AsyncTask<String, String, String>{
#Override
protected String doInBackground(String... uri) {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String responseString = null;
try {
response = httpclient.execute(new HttpGet(uri[0]));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
responseString = out.toString();
out.close();
} else{
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
//TODO Handle problems..
} catch (IOException e) {
//TODO Handle problems..
}
return responseString;
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
//Do anything with response..
}
}
You then can make a request by:
new RequestTask().execute("http://stackoverflow.com");
unless you have an explicit reason to choose the Apache HttpClient, you should prefer java.net.URLConnection. you can find plenty of examples of how to use it on the web.
we've also improved the Android documentation since your original post: http://developer.android.com/reference/java/net/HttpURLConnection.html
and we've talked about the trade-offs on the official blog: http://android-developers.blogspot.com/2011/09/androids-http-clients.html
Note: The Apache HTTP Client bundled with Android is now deprecated in favor of HttpURLConnection. Please see the Android Developers Blog for more details.
Add <uses-permission android:name="android.permission.INTERNET" /> to your manifest.
You would then retrieve a web page like so:
URL url = new URL("http://www.android.com/");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
readStream(in);
}
finally {
urlConnection.disconnect();
}
I also suggest running it on a separate thread:
class RequestTask extends AsyncTask<String, String, String>{
#Override
protected String doInBackground(String... uri) {
String responseString = null;
try {
URL url = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if(conn.getResponseCode() == HttpsURLConnection.HTTP_OK){
// Do normal input or output stream reading
}
else {
response = "FAILED"; // See documentation for more info on response handling
}
} catch (ClientProtocolException e) {
//TODO Handle problems..
} catch (IOException e) {
//TODO Handle problems..
}
return responseString;
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
//Do anything with response..
}
}
See the documentation for more information on response handling and POST requests.
The most simple way is using the Android lib called Volley
Volley offers the following benefits:
Automatic scheduling of network requests. Multiple concurrent network
connections. Transparent disk and memory response caching with
standard HTTP cache coherence. Support for request prioritization.
Cancellation request API. You can cancel a single request, or you can
set blocks or scopes of requests to cancel. Ease of customization, for
example, for retry and backoff. Strong ordering that makes it easy to
correctly populate your UI with data fetched asynchronously from the
network. Debugging and tracing tools.
You can send a http/https request as simple as this:
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://www.yourapi.com";
JsonObjectRequest request = new JsonObjectRequest(url, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
if (null != response) {
try {
//handle your response
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
queue.add(request);
In this case, you needn't consider "running in the background" or "using cache" yourself as all of these has already been done by Volley.
Use Volley as suggested above. Add following into build.gradle (Module: app)
implementation 'com.android.volley:volley:1.1.1'
Add following into AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
And add following to you Activity code:
public void httpCall(String url) {
RequestQueue queue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
// enjoy your response
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// enjoy your error status
}
});
queue.add(stringRequest);
}
It replaces http client and it is very simple.
private String getToServer(String service) throws IOException {
HttpGet httpget = new HttpGet(service);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
return new DefaultHttpClient().execute(httpget, responseHandler);
}
Regards
With a thread:
private class LoadingThread extends Thread {
Handler handler;
LoadingThread(Handler h) {
handler = h;
}
#Override
public void run() {
Message m = handler.obtainMessage();
try {
BufferedReader in =
new BufferedReader(new InputStreamReader(url.openStream()));
String page = "";
String inLine;
while ((inLine = in.readLine()) != null) {
page += inLine;
}
in.close();
Bundle b = new Bundle();
b.putString("result", page);
m.setData(b);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
handler.sendMessage(m);
}
}
As none of the answers described a way to perform requests with OkHttp, which is very popular http client nowadays for Android and Java in general, I am going to provide a simple example:
//get an instance of the client
OkHttpClient client = new OkHttpClient();
//add parameters
HttpUrl.Builder urlBuilder = HttpUrl.parse("https://www.example.com").newBuilder();
urlBuilder.addQueryParameter("query", "stack-overflow");
String url = urlBuilder.build().toString();
//build the request
Request request = new Request.Builder().url(url).build();
//execute
Response response = client.newCall(request).execute();
The clear advantage of this library is that it abstracts us from some low level details, providing more friendly and secure ways to interact with them. The syntax is also simplified and permits to write nice code.
I made this for a webservice to requerst on URL, using a Gson lib:
Client:
public EstabelecimentoList getListaEstabelecimentoPorPromocao(){
EstabelecimentoList estabelecimentoList = new EstabelecimentoList();
try{
URL url = new URL("http://" + Conexao.getSERVIDOR()+ "/cardapio.online/rest/recursos/busca_estabelecimento_promocao_android");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
if (con.getResponseCode() != 200) {
throw new RuntimeException("HTTP error code : "+ con.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader((con.getInputStream())));
estabelecimentoList = new Gson().fromJson(br, EstabelecimentoList.class);
con.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
return estabelecimentoList;
}
Look at this awesome new library which is available via gradle :)
build.gradle: compile 'com.apptakk.http_request:http-request:0.1.2'
Usage:
new HttpRequestTask(
new HttpRequest("http://httpbin.org/post", HttpRequest.POST, "{ \"some\": \"data\" }"),
new HttpRequest.Handler() {
#Override
public void response(HttpResponse response) {
if (response.code == 200) {
Log.d(this.getClass().toString(), "Request successful!");
} else {
Log.e(this.getClass().toString(), "Request unsuccessful: " + response);
}
}
}).execute();
https://github.com/erf/http-request
This is the new code for HTTP Get/POST request in android. HTTPClient is depricated and may not be available as it was in my case.
Firstly add the two dependencies in build.gradle:
compile 'org.apache.httpcomponents:httpcore:4.4.1'
compile 'org.apache.httpcomponents:httpclient:4.5'
Then write this code in ASyncTask in doBackground method.
URL url = new URL("http://localhost:8080/web/get?key=value");
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.setRequestMethod("GET");
int statusCode = urlConnection.getResponseCode();
if (statusCode == 200) {
InputStream it = new BufferedInputStream(urlConnection.getInputStream());
InputStreamReader read = new InputStreamReader(it);
BufferedReader buff = new BufferedReader(read);
StringBuilder dta = new StringBuilder();
String chunks ;
while((chunks = buff.readLine()) != null)
{
dta.append(chunks);
}
}
else
{
//Handle else
}
For me, the easiest way is using library called Retrofit2
We just need to create an Interface that contain our request method, parameters, and also we can make custom header for each request :
public interface MyService {
#GET("users/{user}/repos")
Call<List<Repo>> listRepos(#Path("user") String user);
#GET("user")
Call<UserDetails> getUserDetails(#Header("Authorization") String credentials);
#POST("users/new")
Call<User> createUser(#Body User user);
#FormUrlEncoded
#POST("user/edit")
Call<User> updateUser(#Field("first_name") String first,
#Field("last_name") String last);
#Multipart
#PUT("user/photo")
Call<User> updateUser(#Part("photo") RequestBody photo,
#Part("description") RequestBody description);
#Headers({
"Accept: application/vnd.github.v3.full+json",
"User-Agent: Retrofit-Sample-App"
})
#GET("users/{username}")
Call<User> getUser(#Path("username") String username);
}
And the best is, we can do it asynchronously easily using enqueue method
I want to get the result of getServerResult() method but return "null". However, the Logcat inside the httpURLConnectionPost() method is normal which return "sucessful". Please answer me! why I got null outside the httpURLConnectionPost() method with getServerResult() method in this case?
private String serverResult;
public void httpURLConnectionPost(final String urlString){
new Thread(new Runnable() {
public void run() {
try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.connect();
String body = stringBuilder();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));
writer.write(body);
writer.close();
int responseCode = connection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK){
InputStream inputStream = connection.getInputStream();
String line;
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
while ((line = bufferedReader.readLine()) != null) {
stringBuilderResult.append(line);
}
String result = stringBuilderResult.toString();
Log.d("MSG","result= "+result);
setServerResult(result);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
public void setServerResult(String serverResult){
this.serverResult = serverResult;
}
public String getServerResult(){
return serverResult;
}
D/MSG: result = successful
using getServerResult() :
D/TAG: result = null
Where are you calling httpURLConnectionPost? Regardless you should be using an async task for this, functions that call HTTP requests are not going to return anything because the code is executing and finishing before the response is received.
You need to make sure the task is complete before trying to get any data. Usually you would use an async task and in the onPostExecute portion you would do something to let the app know that data is returned.
I'm tying to read data from server I'm using xampp) but the data is empty
this is my connect activity:
public String link="";
public AsyncTaskConnect(String link){
this.link=link;
}
#Override
protected Object doInBackground(Object[] params) {
try{
URL url=new URL(link);
URLConnection connection=url.openConnection();
BufferedReader reader=new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder builder=new StringBuilder();
String line=null;
while((line=reader.readLine())!=null){
builder.append(line);
}
MainActivity.data=builder.toString();
}catch (Exception e){
}
return "";
}
this is main activity:
public static String data="";
TextView txthello;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txthello=(TextView)findViewById(R.id.txthello);
new AsyncTaskConnect("http://192.168.1.2/digikala/test.php").execute();
txthello.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,data,Toast.LENGTH_LONG).show();
}
});
}
but it doesn't work, what should I do?
but the data is empty
Because execute is not a blocking call.
Assuming you can actually reach the server, MainActivity.data is an empty String until after the Asynctask onPostExecute
You can either use Volley, Okhttp, Retrofit, etc to simplify your networking code
Comparison of Android networking libraries: OkHTTP, Retrofit, and Volley
or add callbacks into your Asynctask
How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?
Using HttpURLConnection, it extends your URLConnection so I am changing just a little to your code. Given you have your query in the String variable link, this should work just as fine.
try {
URL url = new URL(link);
HttpURLConnection connection= (HttpURLConnection) url.openConnection();
int responseCode = connection.getResponseCode();
Log.i(TAG, "POST Response Code: " + responseCode);
//Takes data only if response from WebService is OK
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
//Stores input line by line in response
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
if you follow this snippet, response is the string which contains all of the response you get from your webservice, you can further convert it to JSON if you want.
Hope it works!
After researches i still cant send a JSON POST request to a server.
I already tried some older answers:
Java - sending HTTP parameters via POST method easily
[Android]-POST Json with HttpUrlConnection
Post request for registering user data on server by HttpUrlConnection
Sending json object via http post method in android
How to send a JSON object over Request with Android?
My current code is:
FloatingActionButton btn_sendMsg = (FloatingActionButton) findViewById(R.id.btn_sendMsg);
btn_sendMsg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
/*Snackbar.make(view, "Sendevorgang...", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();*/
createMsg();
}
});
private void createMsg() {
Message message = new Message(txtbox_msg.getText().toString(), "testUser");
AsyncT asyncT = new AsyncT();
asyncT.execute(message);
}
AsyncT.java :
#Override
protected Message doInBackground(Message... params) {
try {
URL url = new URL("http://[ip]:[port]"); //in the real code, there is an ip and a port
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept","application/json");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.connect();
JSONObject jsonParam = new JSONObject();
jsonParam.put("uname", params[0].getUser());
jsonParam.put("message", params[0].getMessage());
jsonParam.put("latitude", "0");
jsonParam.put("longitude", "0");
jsonParam.put("id", "1");
DataOutputStream os = new DataOutputStream(conn.getOutputStream());
os.writeBytes(URLEncoder.encode(jsonParam.toString(), "UTF-8"));
os.flush();
os.close();
Log.i("STATUS", String.valueOf(conn.getResponseCode()));
Log.i("MSG" , conn.getResponseMessage());
conn.disconnect();
} catch (Exception e) {
}
return null;
}
I'm getting the error code networkonmainthreadexception 500
How can i solve this?
Solved:
changed
os.writeBytes(URLEncoder.encode(jsonParam.toString(), "UTF-8"));
to
os.writeBytes(jsonParam.toString());
And put the code in a thread (thanks to #Ravi Sanker)
Working code:
public void sendPost() {
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
try {
URL url = new URL(urlAdress);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
conn.setRequestProperty("Accept","application/json");
conn.setDoOutput(true);
conn.setDoInput(true);
JSONObject jsonParam = new JSONObject();
jsonParam.put("timestamp", 1488873360);
jsonParam.put("uname", message.getUser());
jsonParam.put("message", message.getMessage());
jsonParam.put("latitude", 0D);
jsonParam.put("longitude", 0D);
Log.i("JSON", jsonParam.toString());
DataOutputStream os = new DataOutputStream(conn.getOutputStream());
//os.writeBytes(URLEncoder.encode(jsonParam.toString(), "UTF-8"));
os.writeBytes(jsonParam.toString());
os.flush();
os.close();
Log.i("STATUS", String.valueOf(conn.getResponseCode()));
Log.i("MSG" , conn.getResponseMessage());
conn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
Can you try with writing this in the createMsg() method:
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
try {
// The code written in doInBackground()
} catch (Exception e) {
e.printStackTrace();
}
}});
thread.start();
The networkonmainthread exception comes when you run the network operations on the same thread. But, since you're using an async task, it should work fine. But, just to confirm.
I have developed a web service in Java. Below is a method of it.
#Path("/setup")
public class SetupJSONService {
#POST
#Path("/insertSetup")
#Consumes(MediaType.APPLICATION_JSON)
public String insertSetup(SetupBean bean)
{
System.out.println("Printed");
SetupInterface setupInterface = new SetupImpl();
String insertSetup = setupInterface.insertSetup(bean);
return insertSetup;
}
}
Below is how I call this method using Java Jersey in my computer.
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080/TestApp/rest/setup").path("/insertSetup");
SetupBean setupBean = new SetupBean();
setupBean.setIdPatient(1);
setupBean.setCircleType(1);
target.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.entity(setupBean, MediaType.APPLICATION_JSON_TYPE));
However, Now this method should be called in Android as well, but I'm not sure how to do that. I know how to make GET calls in android like below.
public static String httpGet(String urlStr) throws IOException {
URL url = new URL(urlStr);
HttpURLConnection conn =
(HttpURLConnection) url.openConnection();
if (conn.getResponseCode() != 200) {
throw new IOException(conn.getResponseMessage());
}
// Buffer the result into a string
BufferedReader rd = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = rd.readLine()) != null) {
sb.append(line);
}
rd.close();
conn.disconnect();
return sb.toString();
}
But since my method is POST and since it accept a Java Bean and it does return a String, how can I handle this in Android? Not interested using Jersey in android as it does have bad comments in Android environment.
Android provides a way to do what you want, but this is not a productive way, i like to use retrofit 2 to power my development and to write a better code.
Here a example of retrofit 2 that can help you =) :
add to your dependencies in build.gradle
dependencies {
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
}
Create your retrofit builder that specifies a converter and a base url.
public static final String URL = "http://localhost:8080/TestApp/rest/";
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
Now create a Interface that will encapsulate your rest methods like below
public interface YourEndpoints {
#POST("setup/insertSetup")
Call<ResponseBody> insertSetup(#Body SetupBean setupBean);
}
Associate your endpoints interface with your retrofit instance.
YourEndpoints request = retrofit.create(YourEndpoints.class);
Call<ResponseBody> yourResult = request.insertSetup(YourSetupBeanObject);
yourResult.enqueue(new Callback<ResponseBody>() {
#Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
//response.code()
//your string response response.body().string()
}
#Override
public void onFailure(Throwable t) {
//do what you have to do if it return a error
}
});
Ref to this links for more information:
http://square.github.io/retrofit/
https://github.com/codepath/android_guides/wiki/Consuming-APIs-with-Retrofit
that`s the code for the normal way you want
InputStream is = null;
OutputStream os = null;
HttpURLConnection con = null;
try {
//constants
URL url = new URL("http://localhost:8080/TestApp/rest/");
//Map your object to JSONObject and convert it to a json string
String message = new JSONObject().toString();
con = (HttpURLConnection) url.openConnection();
con.setReadTimeout(1000);
con.setConnectTimeout(15000);
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
con.setFixedLengthStreamingMode(message.getBytes().length);
con.setRequestProperty("Content-Type", "application/json;charset=utf-8");
//open
con.connect();
//setup send
os = new BufferedOutputStream(con.getOutputStream());
os.write(message.getBytes());
//clean up
os.flush();
//do somehting with response
is = con.getInputStream();
String contentAsString = readData(is,len);
os.close();
is.close();
con.disconnect();
} catch (Exception e){
try {
os.close();
is.close();
con.disconnect();
} catch (IOException e1) {
e1.printStackTrace();
}
}