Last active
December 16, 2015 03:49
-
-
Save aalinat/5373181 to your computer and use it in GitHub Desktop.
android http post/get requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.single; | |
import java.io.IOException; | |
import java.io.UnsupportedEncodingException; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.NameValuePair; | |
import org.apache.http.ParseException; | |
import org.apache.http.client.ClientProtocolException; | |
import org.apache.http.client.HttpClient; | |
import org.apache.http.client.entity.UrlEncodedFormEntity; | |
import org.apache.http.client.methods.HttpGet; | |
import org.apache.http.client.methods.HttpPost; | |
import org.apache.http.client.methods.HttpUriRequest; | |
import org.apache.http.impl.client.DefaultHttpClient; | |
import org.apache.http.message.BasicNameValuePair; | |
import org.apache.http.protocol.BasicHttpContext; | |
import org.apache.http.protocol.HTTP; | |
import org.apache.http.protocol.HttpContext; | |
import org.apache.http.util.EntityUtils; | |
import android.os.AsyncTask; | |
import android.os.Bundle; | |
import android.app.Activity; | |
import android.view.Menu; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.ProgressBar; | |
import android.widget.TextView; | |
public class MainActivity extends Activity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
final Button button = (Button) findViewById(R.id.button1); | |
button.setOnClickListener(new View.OnClickListener() { | |
public void onClick(View v) { | |
final ProgressBar progress = (ProgressBar)findViewById(R.id.progressBar1); | |
progress.setVisibility(View.VISIBLE); | |
new Backup().execute("http://kwebapps.com/service.php","get"); | |
} | |
}); | |
final Button button2 = (Button)findViewById(R.id.BtnSendName); | |
button2.setOnClickListener(new View.OnClickListener(){ | |
@Override | |
public void onClick(View v) { | |
final ProgressBar progress = (ProgressBar)findViewById(R.id.progressBar1); | |
progress.setVisibility(View.VISIBLE); | |
new Backup().execute("http://kwebapps.com/service.php","post"); | |
} | |
}); | |
} | |
private class Backup extends AsyncTask<String, Void, String> { | |
private HttpPost getPostRequest(String url){ | |
HttpPost method = new HttpPost(url); | |
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); | |
EditText text =(EditText) findViewById(R.id.TextUsername); | |
nameValuePairs.add(new BasicNameValuePair("username",text.getText().toString() )); | |
try { | |
method.setEntity(new UrlEncodedFormEntity(nameValuePairs)); | |
} catch (UnsupportedEncodingException e1) { | |
// TODO Auto-generated catch block | |
e1.printStackTrace(); | |
} | |
return method; | |
} | |
private HttpGet getGetRequest(String url){ | |
return new HttpGet(url); | |
} | |
@Override | |
protected String doInBackground(String... args) { | |
HttpClient httpClient = new DefaultHttpClient(); | |
HttpContext localContext = new BasicHttpContext(); | |
HttpUriRequest m = null; | |
if (args[1] == "post"){ | |
m =getPostRequest(args[0]); | |
}else{ | |
m =getGetRequest(args[0]); | |
} | |
HttpResponse response = null; | |
String result = ""; | |
try { | |
response = httpClient.execute(m , localContext); | |
} catch (ClientProtocolException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
try { | |
return EntityUtils.toString(response.getEntity(),HTTP.UTF_8); | |
} catch (ParseException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
return ""; | |
} | |
protected void onPostExecute(String result) { | |
final ProgressBar progress = (ProgressBar)findViewById(R.id.progressBar1); | |
TextView text = (TextView)findViewById(R.id.textView1); | |
text.setText(result); | |
progress.setVisibility(View.INVISIBLE); | |
} | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.main, menu); | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
tools:context=".MainActivity" > | |
<Button | |
android:id="@+id/button1" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignParentRight="true" | |
android:layout_alignParentTop="true" | |
android:layout_marginRight="18dp" | |
android:text="Request Date" /> | |
<TextView | |
android:id="@+id/textView1" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignBaseline="@+id/button1" | |
android:layout_alignBottom="@+id/button1" | |
android:layout_alignParentLeft="true" | |
android:layout_marginLeft="19dp" | |
android:text="@string/hello_world" /> | |
<Button | |
android:id="@+id/BtnSendName" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_above="@+id/progressBar1" | |
android:layout_alignRight="@+id/TextUsername" | |
android:layout_marginBottom="14dp" | |
android:layout_marginRight="44dp" | |
android:text="Send Name" /> | |
<EditText | |
android:id="@+id/TextUsername" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_above="@+id/BtnSendName" | |
android:layout_centerHorizontal="true" | |
android:layout_marginBottom="21dp" | |
android:ems="10" > | |
<requestFocus /> | |
</EditText> | |
<ProgressBar | |
android:id="@+id/progressBar1" | |
style="?android:attr/progressBarStyleLarge" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignParentBottom="true" | |
android:layout_centerHorizontal="true" | |
android:layout_marginBottom="146dp" | |
android:visibility="invisible" /> | |
</RelativeLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment