Showing posts with label android. Show all posts
Showing posts with label android. Show all posts

Thursday, 12 September 2013

Android - How to use HttpURLConnection to send POST request to server

I'm developing an Android app, with which the user can log in and do their task. To log in, it has to connect to the server. I've searched the internet, but most of the examples are about using GET method, not POST. So after I solved my problem, I write this post for those looking for a similar solution.
1. write this in your manifest.xml:
    <uses-permission android:name="android.permission.INTERNET" />
2. server side:
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String name = request.getParameter("name");
PrintWriter out = response.getWriter();
out.println("Hello "+name);
}
3. Android side, instead of using the main thread, you need to execute the connection in the background. First, implement your own thread:
private class BKTask extends AsyncTask<String, Void, String> {
private String name="";
@Override
protected String doInBackground(String... urls) {
String output = null;
for (String url : urls) {
output = getOutputFromUrl(url);
}
return output;
}

private String getOutputFromUrl(String url) {
StringBuffer output = new StringBuffer("");
try {
InputStream stream = getHttpConnection(url);
BufferedReader buffer = new BufferedReader(
new InputStreamReader(stream));
String s = "";
while ((s = buffer.readLine()) != null)
output.append(s);
} catch (IOException e1) {
e1.printStackTrace();
}
return output.toString();
}

// Makes HttpURLConnection and returns InputStream
private InputStream getHttpConnection(String urlString)
throws IOException {
InputStream stream = null;
URL url = new URL(urlString);
URLConnection connection = url.openConnection();

try {
HttpURLConnection httpConnection = (HttpURLConnection) connection;
httpConnection.setRequestMethod("POST");
httpConnection.setDoOutput(true);
httpConnection.connect();
//post

OutputStreamWriter writer = new OutputStreamWriter(httpConnection.getOutputStream());
String urlParameters = "name="+name;
writer.write(urlParameters);
writer.flush();

if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
stream = httpConnection.getInputStream();
}
writer.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return stream;
}

@Override
protected void onPostExecute(String output) {

msg.setText(output);
}
}
4. how to use this thread? See the following code. I use a button to submit the request, so this is my code which is called when the button is clicked:
public void submit(View v){
BKTask task = new BKTask();
task.name = name.getText().toString();
task.execute(new String[] { URL });
}

That's all. If any questions, please leave me a msg:)

Sunday, 23 December 2012

How to set weight in linearlayout in Android

Today I was writing an app on Android. There was a LinearLayout with 3 TextViews which fill the screen. I wanted their width to be 1:6:6. So I set the "android:layout_weight" property 1, 6, 6. But it didn't work that way. The first TextView became very big and the other two become very small. Then I guess the "weight" means importance. The smaller the weight is, the larger it will be. So I read a lot and found this method to set the weights of the components in your app:
1. Suppose the size of the screen is X.
2. Count how many views there are in a LinearLayout, here I use my example, 3.
3. Total width: total = 3X.
4. delta = X - total = -2X.
5. Suppose you set the weight of the first TextView to be A.
6. Suppose you want the TextView to be 1/13 of the width of the screen.
7. Then use this formula to solve the value of 'A':  X + delta*(A/13) = (1/13)X , A=6

I did like this and got the three weight values: 6, 3.5, 3.5, and I got the result exactly as I wanted, the width of the three TextView is 1:6:6.