2017-08-08 70 views
0

我想從json到php到android。我嘗試越來越多,但現在我的應用程序'它不工作,當我按此佈局。我真的嘗試過並搜索它,但搜索的結果並沒有幫助我。如何讓json從php到android?

我想得到一些單詞或句子到我的佈局它像我們一樣。

我的佈局

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<TextView 
    android:id="@+id/how_are_us_TopTextView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/howRUS" 
    android:textAlignment="center" 
    android:textAllCaps="false" 
    android:textColor="@android:color/holo_red_dark" 
    android:textStyle="bold" 
    android:textSize="27sp" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="19dp" /> 

<TextView 
    android:id="@+id/informationAboutUS" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/how_are_us_TopTextView" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="73dp" 
    android:text="@string/student" 
    android:textAlignment="center" 
    android:textColor="@android:color/holo_red_dark" 
    android:textSize="24sp" 
    android:textStyle="bold" /> 

<Button 
    android:id="@+id/how_are_us_ButtonGoToWebsite" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/informationAboutUS" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="69dp" 
    android:text="@string/goToWebsite" 
    android:textAlignment="center" 
    android:textColor="@android:color/holo_green_dark" 
    android:textSize="16sp" 
    android:textStyle="bold" /> 

我的PHP jsonGetHRSInformation

<meta charset="UTF-8"> 
<?php 

$myObj->student = "some thing like about us"; 


$myJSON = json_encode($myObj); 

echo $myJSON; 
?> 

我的課

private final String urlStr = "http://.... /howAreUsJson.php"; 

public String getData() throws IOException{ 
    URL url = new URL(urlStr); 
    HttpURLConnection connection= (HttpURLConnection) url.openConnection(); 

    try { 
     BufferedReader input = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
     String line ; 
     StringBuilder sb = new StringBuilder(); 
     while ((line = input.readLine()) != null){ 
      sb.append(line); 
     } 
     return sb.toString(); 
    } 
    catch (IOException e){ 

     throw e; 
    }finally { 
     if(connection != null){ 
      connection.disconnect(); 
      connection = null; 
     } 
    } 
} 

我的片段

private TextView textView; 
private Handler handler; 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.how_are_us, null); 
    return view; 
} 

@Override 
public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 
    textView = (TextView) view.findViewById(R.id.informationAboutUS); 
    handler = new Handler(Looper.getMainLooper()); 
    Thread runner = new Thread(new Runnable() { 
     @Override 
     public void run() { 
      GetHRSInformation getHRSInformation = new GetHRSInformation(); 

      try{ 
       String res = getHRSInformation.getData(); 
       JSONObject json = new JSONObject(res); 
       final JSONArray informationArray = json.getJSONArray("student"); 

       handler.post(new Runnable() { 
        @Override 
        public void run() { 
         textView.setText("student: " + informationArray); 
        } 
       }); 


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

     } 
    }); 


    runner.start(); 

} 

回答

0

我認爲它是因爲你的AR使用

final JSONArray informationArray = json.getJSONArray("student"); 

嘗試使用

final String informationArray = json.getString("student"); 

我建議你使用GSON來解析JSON並避免使用「線程」,你應該使用「 AsyncTask「,而不是

+0

謝謝你,改變代碼後,哈德一些問題,我被改爲'String res = getHRSInformation.getData(); final String informationArray = res.concat(「student」);'我在PHP代碼行4有問題:'$ myObj-> student =「有些事情就像我們一樣」;' – hasan