-1

im new on Android Code, I need this for connect to internet and READ a JSON text from ASPX (Visual .NET for Websites) and Deserialize but my app crash when load the unic layout.

The JSON of the website doesnt have Arrays, I need help please!

This is on the ActivityMain.Java:

    TextView wid = (TextView) findViewById(R.id.wid);
    TextView name = (TextView) findViewById(R.id.name);
    TextView url = (TextView) findViewById(R.id.url);

    String str = ""; 
    JSONObject json = null;

    HttpResponse response;
    HttpClient myClient = new DefaultHttpClient();
    HttpPost URL = new HttpPost("http://validafacturas.com/BuzonFacturas/Account/Loginmb.aspx");

    try {
        response = myClient.execute(URL);
        str = EntityUtils.toString(response.getEntity(), "UTF-8");

    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try{

        JSONArray jArray = new JSONArray(str);
        json = jArray.getJSONObject(0);

        wid.setText(json.getString("success"));
        name.setText(json.getString("message"));
        url.setText(json.getString("iduser"));

    } catch ( JSONException e) {
        e.printStackTrace();                
    }         
}

  1. The error when i use the "Debug" on the Eclipse, I see a Break-Point on "HTTP Response" (red dot), I test this app on Emulator & Xperia Sola in a connection in wi-fi
  2. I put the Internet Permission on the Manifest
  3. Thanks by the URL for JSON, but I don't have any trouble to understand the JSON. Thanks any way
  • Use [AsyncTask](http://developer.android.com/reference/android/os/AsyncTask.html) for calling api. currently calling api on main ui thread – ρяσѕρєя K Feb 26 '15 at 20:31
  • 1
    You didn't provide an error message. Don't assume we know what error you got, please edit it into your question. – mason Feb 26 '15 at 20:42
  • If you are having trouble understanding JSON, try this... http://json.parser.online.fr/ – user2260040 Feb 26 '15 at 21:01

1 Answers1

0

First, you must use AsyncTask, your code do not work on recent Android's version.

Parse JSON with a library, like google-gson

{"success":"false", "message":"Credenciales de acceso inválidas. Inténtelo de nuevo.", "iduser":"0"}


class DataWrapper {
        public Data data;

        public static DataWrapper fromJson(String s) {
            return new Gson().fromJson(s, DataWrapper.class);
        }
        public String toString() {
            return new Gson().toJson(this);
        }
    }
    class Data {
        public boolean success;
        public String message;
        public int idUser;
    }   
agustinm20
  • 138
  • 1
  • 7
  • Can I use the kSOAP2 for Deserialize the JSON?, thanks by the way – Gabriel Durruti Feb 26 '15 at 21:13
  • Exactly! And auto-generate the mapping objects with WSDL2Code ;) – agustinm20 Feb 26 '15 at 21:27
  • How cain I do that?, i'm tried for hours with WSDL2, I just need read and Deserialize this JSON [link](https://validafacturas.com/BuzonFacturas/Account/Loginmb.aspx), that's all, again thanks – Gabriel Durruti Feb 27 '15 at 01:28
  • Sorry, you don't have a WSDL, that's why you can't use WSDL2Code. You need to create a Java class and parse the JSON, like this: http://stackoverflow.com/questions/6079505/converting-json-string-to-java-object using this library its easy: https://code.google.com/p/google-gson/ – agustinm20 Feb 27 '15 at 15:31