0

I am a very new to android and have followed various tutorials on making a post request in android programming.What i want to do is loging to https://ubresources.com. below is my java class

Results.java

package com.example.fetapp;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Results extends Activity {
private EditText email, pass;
@Override
public void onCreate(Bundle savedInstanceState){
   super.onCreate(savedInstanceState);
   setContentView(R.layout.result);
     email = (EditText)findViewById(R.id.email);
     pass  = (EditText)findViewById(R.id.pass);
   Button login = (Button)findViewById(R.id.login);

   login.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        postLoginData();
        startActivity(new Intent(Results.this, MainPage.class));
    }
});
}

public void postLoginData() {
   // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("https://ubresources.com/login.json");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("username", email.getText().toString);
        nameValuePairs.add(new BasicNameValuePair("password", pass.getText().toString);
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        Log.d("Login", response.toString());

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }

} }

but still cannot get the response.Please help me out,my logcat info looks is

09-11 17:35:54.244: I/Choreographer(1197): Skipped 73 frames!  The application may be doing too much work on its main thread.
09-11 17:35:54.284: D/gralloc_goldfish(1197): Emulator without GPU emulation detected.
09-11 17:35:55.916: I/Choreographer(1197): Skipped 90 frames!  The application may be doing too much work on its main thread.
09-11 17:35:56.014: I/Choreographer(1197): Skipped 56 frames!  The application may be doing too much work on its main thread.
09-11 17:35:56.114: I/Choreographer(1197): Skipped 46 frames!  The application may be doing too much work on its main thread.
09-11 17:35:56.224: I/Choreographer(1197): Skipped 53 frames!  The application may be doing too much work on its main thread.
09-11 17:35:56.324: I/Choreographer(1197): Skipped 46 frames!  The application may be doing too much work on its main thread.
09-11 17:35:56.434: I/Choreographer(1197): Skipped 42 frames!  The application may be doing too much work on its main thread.
09-11 17:35:56.553: I/Choreographer(1197): Skipped 71 frames!  The application may be doing too much work on its main thread.
09-11 17:35:56.653: I/Choreographer(1197): Skipped 65 frames!  The application may be doing too much work on its main thread.
09-11 17:35:56.763: I/Choreographer(1197): Skipped 52 frames!  The application may be doing too much work on its main thread.
09-11 17:35:56.853: I/Choreographer(1197): Skipped 41 frames!  The application may be doing too much work on its main thread.
09-11 17:35:57.204: I/Choreographer(1197): Skipped 39 frames!  The application may be doing too much work on its main thread.
09-11 17:35:57.474: D/dalvikvm(1197): GC_FOR_ALLOC freed 217K, 5% free 6298K/6599K, paused 63ms, total 66ms
09-11 17:35:57.593: D/dalvikvm(1197): GC_FOR_ALLOC freed 198K, 6% free 6384K/6727K, paused 68ms, total 69ms
09-11 17:35:57.623: I/dalvikvm-heap(1197): Grow heap (frag case) to 7.392MB for 1165616-byte allocation
09-11 17:35:57.773: D/dalvikvm(1197): GC_CONCURRENT freed <1K, 5% free 7522K/7879K, paused 22ms+29ms, total 153ms
09-11 17:35:57.944: D/dalvikvm(1197): GC_FOR_ALLOC freed 0K, 5% free 7523K/7879K, paused 60ms, total 60ms
09-11 17:35:57.953: I/dalvikvm-heap(1197): Grow heap (frag case) to 7.887MB for 519172-byte allocation
09-11 17:35:58.074: D/dalvikvm(1197): GC_CONCURRENT freed 0K, 5% free 8030K/8391K, paused 20ms+9ms, total 120ms
09-11 17:35:58.074: D/dalvikvm(1197): WAIT_FOR_CONCURRENT_GC blocked 32ms
09-11 17:35:58.454: I/Choreographer(1197): Skipped 118 frames!  The application may be doing too much work on its main thread.
09-11 17:35:59.164: I/Choreographer(1197): Skipped 193 frames!  The application may be doing too much work on its main thread.
09-11 17:36:08.683: I/Choreographer(1197): Skipped 98 frames!  The application may be doing too much work on its main thread.
09-11 17:36:08.803: I/Choreographer(1197): Skipped 63 frames!  The application may be doing too much work on its main thread.
09-11 17:36:08.953: I/Choreographer(1197): Skipped 74 frames!  The application may be doing too much work on its main thread.
09-11 17:36:09.094: I/Choreographer(1197): Skipped 87 frames!  The application may be doing too much work on its main thread.
09-11 17:36:09.204: I/Choreographer(1197): Skipped 67 frames!  The application may be doing too much work on its main thread.
09-11 17:36:09.324: I/Choreographer(1197): Skipped 56 frames!  The application may be doing too much work on its main thread.
09-11 17:36:09.883: I/Choreographer(1197): Skipped 52 frames!  The application may be doing too much work on its main thread.
09-11 17:36:10.004: I/Choreographer(1197): Skipped 56 frames!  The application may be doing too much work on its main thread.
09-11 17:36:10.434: I/Choreographer(1197): Skipped 35 frames!  The application may be doing too much work on its main thread.
09-11 17:36:10.833: I/Choreographer(1197): Skipped 87 frames!  The application may be doing too much work on its main thread.
09-11 17:36:11.573: I/Choreographer(1197): Skipped 154 frames!  The application may be doing too much work on its main thread.
09-11 17:36:16.523: I/Choreographer(1197): Skipped 78 frames!  The application may be doing too much work on its main thread.
09-11 17:36:18.643: D/InputEventConsistencyVerifier(1197): KeyEvent: ACTION_UP but key was not down.
09-11 17:36:18.643: D/InputEventConsistencyVerifier(1197):   in android.widget.EditText@4104d088
09-11 17:36:18.643: D/InputEventConsistencyVerifier(1197):   0: sent at 1890932000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_TAB, scanCode=15, metaState=0, flags=0x8, repeatCount=0, eventTime=1890932, downTime=1890824, deviceId=0, source=0x101 }
09-11 17:36:30.733: I/Choreographer(1197): Skipped 36 frames!  The application may be doing too much work on its main thread.
09-11 17:36:33.234: I/Choreographer(1197): Skipped 47 frames!  The application may be doing too much work on its main thread.
09-11 17:36:34.583: D/AndroidRuntime(1197): Shutting down VM
09-11 17:36:34.583: W/dalvikvm(1197): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
09-11 17:36:34.653: E/AndroidRuntime(1197): FATAL EXCEPTION: main
09-11 17:36:34.653: E/AndroidRuntime(1197): android.os.NetworkOnMainThreadException
09-11 17:36:34.653: E/AndroidRuntime(1197):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
09-11 17:36:34.653: E/AndroidRuntime(1197):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
09-11 17:36:34.653: E/AndroidRuntime(1197):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
09-11 17:36:34.653: E/AndroidRuntime(1197):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
09-11 17:36:34.653: E/AndroidRuntime(1197):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
09-11 17:36:34.653: E/AndroidRuntime(1197):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
09-11 17:36:34.653: E/AndroidRuntime(1197):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
09-11 17:36:34.653: E/AndroidRuntime(1197):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
09-11 17:36:34.653: E/AndroidRuntime(1197):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
09-11 17:36:34.653: E/AndroidRuntime(1197):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
09-11 17:36:34.653: E/AndroidRuntime(1197):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
09-11 17:36:34.653: E/AndroidRuntime(1197):     at com.example.fetapp.Results.postLoginData(Results.java:61)
09-11 17:36:34.653: E/AndroidRuntime(1197):     at com.example.fetapp.Results$1.onClick(Results.java:42)
09-11 17:36:34.653: E/AndroidRuntime(1197):     at android.view.View.performClick(View.java:4084)
09-11 17:36:34.653: E/AndroidRuntime(1197):     at android.view.View$PerformClick.run(View.java:16966)
09-11 17:36:34.653: E/AndroidRuntime(1197):     at android.os.Handler.handleCallback(Handler.java:615)
09-11 17:36:34.653: E/AndroidRuntime(1197):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-11 17:36:34.653: E/AndroidRuntime(1197):     at android.os.Looper.loop(Looper.java:137)
09-11 17:36:34.653: E/AndroidRuntime(1197):     at android.app.ActivityThread.main(ActivityThread.java:4745)
09-11 17:36:34.653: E/AndroidRuntime(1197):     at java.lang.reflect.Method.invokeNative(Native Method)
09-11 17:36:34.653: E/AndroidRuntime(1197):     at java.lang.reflect.Method.invoke(Method.java:511)
09-11 17:36:34.653: E/AndroidRuntime(1197):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-11 17:36:34.653: E/AndroidRuntime(1197):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-11 17:36:34.653: E/AndroidRuntime(1197):     at dalvik.system.NativeStart.main(Native Method)
09-11 17:41:34.804: I/Process(1197): Sending signal. PID: 1197 SIG: 9
09-11 17:41:35.924: E/Trace(1213): error opening trace file: No such file or directory (2)
09-11 17:41:36.934: D/dalvikvm(1213): GC_FOR_ALLOC freed 164K, 5% free 6132K/6407K, paused 87ms, total 91ms
09-11 17:41:37.483: I/Choreographer(1213): Skipped 82 frames!  The application may be doing too much work on its main thread.
09-11 17:41:37.523: D/gralloc_goldfish(1213): Emulator without GPU emulation detected.
ViliusK
  • 11,345
  • 4
  • 67
  • 71
Nappy
  • 81
  • 8
  • Could you add an example of some output? E.g. any error messages you're getting might help other to work out what's going wrong. – Dave Challis Sep 11 '14 at 21:27

4 Answers4

1

Try to use Retrofit library for that. It will also parse json for you. You will need only to create simple java object for your data.

bgplaya
  • 1,105
  • 15
  • 27
1

Did you included the INTERNET permission on your app?

It seems your error happens either because you don't have Internet access or, less likely, permission to access the Internet.

Try reading Android's help about connecting to see if you didn't forget anything: http://developer.android.com/training/basics/network-ops/connecting.html

And make sure to try this, perhaps you're having the same problem: How to connect android emulator to the internet

Community
  • 1
  • 1
Rafael Lins
  • 458
  • 7
  • 12
1

The problem can be directly seen in your logcat.

NetworkOnMainThreadException

E/AndroidRuntime(1197): android.os.**NetworkOnMainThreadException** 09-11 17:36:34.653: 

Here is a reference to the doc that basically says you can't do this.

http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

The DefaultHttpClient is a rather low level class and as such tends to be much harder to use than say something like Retrofit

http://square.github.io/retrofit

Retrofit needs to be downloaded, and does take a little bit of setup, but in the long run you will be much happier using a much more highlevel api for Rest api calls.

Here is another good resource you might be interested in as well. It describes (or at least points you in the direction) of combining Squares's Retrofit and Otto (event bus) into a very nice way to communicate with a Restful web service

http://www.mdswanson.com/blog/2014/04/07/durable-android-rest-clients.html

nPn
  • 16,254
  • 9
  • 35
  • 58
0

Your problem there is you didn't implement AsyncTask when retrieving the response or maybe you did not add the permissions for internet / networking?

Try the android volley, it is the one recommended to use as it is owned by google as well as provides you faster networking access. If you try the android volley I can help you in many ways just reach me through my email or twitter. Cheers!

Mike
  • 1,313
  • 12
  • 21