2016-07-31 5 views
0

여기에 많은 비슷한 질문이 있지만 그 중 하나에서 솔루션을 찾지 못했습니다. 왜냐하면이 모든 경우가 약간 다르기 때문입니다. 나는 안드로이드 프로그래밍에 익숙하지 않으며 PHP를 통해 SQL에서 SQL로 데이터를 가져와야합니다. 이제 튜토리얼이나 코드를 사용해도 오류는 계속 발생합니다. 이 방법은 PHP에서 하나의 JSON 객체를 얻고 브라우저에서 보여주는 단일 JSON 객체를 얻고 응용 프로그램의 TextView에 저장하는 간단한 방법입니다. 하지만 계속 충돌하고 정확히 바꿀 필요가 있는지 알지 못합니다. Android Studio에서 JSON 객체 가져 오기 및 표시

는 PHP 코드 ("meinprofil.php")입니다 :

<meta charset="utf-8"> 
<?php 
     include("../config/config.php"); 

     $mail = '[email protected]'; 
     $abfrage = mysql_query("SELECT * FROM user WHERE user_mail LIKE '".$mail."'") or die(mysql_error()); 
     $user = mysql_fetch_array($abfrage); 

    $profil = array('id' => $user['id_user'], 'name' => $user['user_name'], 'mail' => $user['user_mail'], 'tel' => ['user_tel']); 

     echo json_encode($profil); 
     $json = $profil; 
     return $json; ?> 

이것은 ProfilAnsicht.java 클래스입니다 :

package com.example.android.festivalapp; 

import android.os.AsyncTask; 
import android.os.Bundle; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ImageView; 
import android.widget.TextView; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 

//Main Klasse 
public class Profil extends StartseiteNachLogin { 


    ImageView iVProfilbild; 
    EditText eTUserAlter; 
    Button bProfilAendern; 
    TextView tvUsername1; 

    //Layoutverknüpfung 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.profil); 

     iVProfilbild = (ImageView) findViewById(R.id.iVProfilbild); 
     tvUsername1 = (TextView) findViewById(R.id.tvUsername); 
     eTUserAlter = (EditText) findViewById(R.id.eTUserAlter); 
     bProfilAendern = (Button) findViewById(R.id.bProfilAendern); 

     new JSONTask().execute("http://pou-pou.de/stagedriver/android/meinprofil.php"); 


    } 

    public class JSONTask extends AsyncTask<String, String, String> { 
     @Override 
     protected String doInBackground(String... params) { 

      HttpURLConnection connection = null; 
      BufferedReader reader = null; 
      try { 
       URL url = new URL(params[0]); 
       connection = (HttpURLConnection) url.openConnection(); 
       connection.connect(); 

       InputStream stream = connection.getInputStream(); 

       reader = new BufferedReader(new InputStreamReader(stream)); 
       StringBuffer buffer = new StringBuffer(); 
       String line = ""; 

       while ((line = reader.readLine()) != null) { 
        buffer.append(line); 
       } 
       String finalJson = buffer.toString(); 
       JSONObject parentObject = new JSONObject(finalJson); 
       JSONArray parentArray = parentObject.getJSONArray("profil"); 
       JSONObject finalObject = parentArray.getJSONObject(0); 

       String user_name = finalObject.getString("name"); 

       return user_name; 

      } catch (MalformedURLException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } finally { 
       if (connection != null) { 
        connection.disconnect(); 
       } 
       try { 
        if (reader != null) { 
         reader.close(); 
        } 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      super.onPostExecute(result); 
      TextView tvUsername; 
      tvUsername = (TextView) findViewById(R.id.tvUsername); 
      tvUsername.setText(result); 
     } 
    } 
} 

내가 그것을 실행하면, 나는 다음과 같은 오류를 얻을 :

07-31 12:25:20.314 31348-31348/com.example.android.festivalapp W/System: ClassLoader referenced unknown path: /data/app/com.example.android.festivalapp-1/lib/arm64 
07-31 12:25:20.488 31348-31348/com.example.android.festivalapp W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 
07-31 12:25:20.572 31348-31413/com.example.android.festivalapp D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true 
07-31 12:25:20.596 31348-31413/com.example.android.festivalapp I/Adreno: QUALCOMM build     : bf7710d, I0bc8e21cf2 
                     Build Date      : 03/09/16 
                     OpenGL ES Shader Compiler Version: XE031.06.00.02 
                     Local Branch      : 
                     Remote Branch     : quic/LA.BF64.1.2.2_rb4.28 
                     Remote Branch     : NONE 
                     Reconstruct Branch    : NOTHING 
07-31 12:25:20.599 31348-31413/com.example.android.festivalapp I/OpenGLRenderer: Initialized EGL, version 1.4 
07-31 12:25:20.690 31348-31348/com.example.android.festivalapp I/Timeline: Timeline: Activity_idle id: [email protected] time:118876554 
07-31 12:25:35.764 31348-31348/com.example.android.festivalapp I/Timeline: Timeline: Activity_idle id: [email protected] time:118891628 
07-31 12:25:37.619 31348-31348/com.example.android.festivalapp W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView 
07-31 12:25:38.605 31348-31348/com.example.android.festivalapp I/Timeline: Timeline: Activity_launch_request id:com.example.android.festivalapp time:118894469 
07-31 12:25:38.729 31348-31684/com.example.android.festivalapp I/DpmTcmClient: RegisterTcmMonitor from: com.android.okhttp.TcmIdleTimerMonitor 
07-31 12:25:38.804 31348-31684/com.example.android.festivalapp W/System.err: org.json.JSONException: **Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject** 
07-31 12:25:38.804 31348-31684/com.example.android.festivalapp W/System.err:  at org.json.JSON.typeMismatch(JSON.java:111) 
07-31 12:25:38.804 31348-31684/com.example.android.festivalapp W/System.err:  at org.json.JSONObject.<init>(JSONObject.java:160) 
07-31 12:25:38.804 31348-31684/com.example.android.festivalapp W/System.err:  at org.json.JSONObject.<init>(JSONObject.java:173) 
07-31 12:25:38.804 31348-31684/com.example.android.festivalapp W/System.err:  at com.example.android.festivalapp.Profil$JSONTask.doInBackground(Profil.java:68) 
07-31 12:25:38.804 31348-31684/com.example.android.festivalapp W/System.err:  at com.example.android.festivalapp.Profil$JSONTask.doInBackground(Profil.java:47) 
07-31 12:25:38.804 31348-31684/com.example.android.festivalapp W/System.err:  at android.os.AsyncTask$2.call(AsyncTask.java:295) 
07-31 12:25:38.804 31348-31684/com.example.android.festivalapp W/System.err:  at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
07-31 12:25:38.804 31348-31684/com.example.android.festivalapp W/System.err:  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
07-31 12:25:38.804 31348-31684/com.example.android.festivalapp W/System.err:  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
07-31 12:25:38.804 31348-31684/com.example.android.festivalapp W/System.err:  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
07-31 12:25:38.804 31348-31684/com.example.android.festivalapp W/System.err:  at java.lang.Thread.run(Thread.java:818) 
07-31 12:25:38.860 31348-31413/com.example.android.festivalapp D/OpenGLRenderer: endAllActiveAnimators on 0x5561e47040 (ListPopupWindow$DropDownListView) with handle 0x5561e8f2d0 
07-31 12:25:38.865 31348-31348/com.example.android.festivalapp I/Timeline: Timeline: Activity_idle id: [email protected] time:118894728 
07-31 12:29:07.316 31348-31348/com.example.android.festivalapp I/Timeline: Timeline: Activity_idle id: [email protected] time:119103180 

누군가가 도움을 줄 수 있다면 정말 감사하겠습니다. 여기에 많은 질문과 튜토리얼을 읽었지만 PHP와 JSON을 Java로 연결하고 JSON Object/Array를 표시하는 기본 방법을 이해하지 못했습니다. somone이 "조리법"또는 따라야 할 기본 단계를 알고 있다면 나와 공유 할 수 있다면 정말 감사 할 것입니다.

미리 감사드립니다.

답변

0

는 당신이 그렇지 않으면 결과를 JSON 형식으로 인코딩되지 않기 때문에

$enc_value = json_encode($profil); 
return $enc_value; 

에 PHP 코드를 수정할 필요가 있다고 생각한다.

JSONObject parentObject = new JSONObject(finalJson); 
JSONArray parentArray = parentObject.getJSONArray("profil"); 
JSONObject finalObject = parentArray.getJSONObject(0); 

을 만 사용 : 실제로 자바 측면에서 당신은 제거 할 수 있습니다 star_kid57 @

JSONObject finalObject = new JSONObject(finalJson); 
+0

를 작동합니까? – Lino

+0

불행히도, 나는 그것이 당신이 의미하는 바를 이해합니다. 그러나 여전히 작동하지 않습니다. 로그는 말한다 STH 같은 : org.json.JSONException : org.json.JSONTokener.nextValue (JSONTokener.java에서 org.json.JSONTokener.syntaxError (JSONTokener.java:449)에서 의 문자 0 에서 입력의 끝 : 97) at org.json.JSONObject. (JSONObject.java:156) at org.json.JSONObject. (JSONObject.java:173) 등등. –

+0

안드로이드 logcat에서 같은 오류가 발생합니까? – Lino

관련 문제