[Help] Login Activity using JSON, PHP & MYSQL
ditanyakan oleh Noval Fazriansyah 1 tahun yang lalu
gan minta tolong dong, mentok nih.
ini Logcatnya:
ini source code JSONParsernya:
class JSONParser { private static InputStream is = null; private static JSONObject jObj = null; private static String json = ""; JSONParser() { } JSONObject getJSONFromUrl(String url) { try { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); } catch (IOException e) { e.printStackTrace(); } try { BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { // Log.i("Tag",line); sb.append(line).append("\n"); } is.close(); json = sb.toString(); } catch (Exception e) { Log.e("Buffer_Error", "Error converting result " + e.toString()); } try { jObj = new JSONObject(json); } catch (JSONException e) { Log.e("JSON_Parser", "Error parsing data " + e.toString()); } return jObj; }
ini source code MainActivitynya:
protected String doInBackground(String... arg0) { JSONParser jParser = new JSONParser(); JSONObject json = jParser.getJSONFromUrl(url); try { // if (json.getString(success) != null) { // Log.e("error", "nilai sukses=" + success); // } success = json.getString(success); Log.e("error", "nilai sukses=" + success); JSONArray hasil = json.getJSONArray("login"); if (success.equals(1)) { //if (Integer.parseInt("success") == 1){ for (int i = 0; i < hasil.length(); i++) { JSONObject c = hasil.getJSONObject(i); String username = c.getString("username").trim(); String name = c.getString("name").trim(); session.createLoginSession(username, name); Log.e("ok", "ambil data"); } // Log.e("error", "Ya ambil data ok");} } else { Log.e("Error", "tidak bisa ambil data 0"); } } catch (Exception e) { Log.e("Error", "Tidak bisa ambil data 1"); } return null; }