2016-08-01 218 views
-1

我能夠讀取JSON,但現在我只想讀取一些值。我會ID和Android應該給我的用戶名和密碼。或者我會給用戶名和android應該提供給我相應的ID和密碼。 這裏是我的代碼: -從JSON數組中提取數據android

Android:-MainActivity.Java 

package com.example.nupur.readjsonexample; 

import android.app.ProgressDialog; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.text.method.ScrollingMovementMethod; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.URL; 


public class MainActivity extends ActionBarActivity implements View.OnClickListener { 
    EditText etcheck; 

    private TextView textViewJSON; 
    private Button buttonGet; 
    // TextView txtCheck; 
    //private Button buttonParse; 

    public static final String MY_JSON ="MY_JSON"; 

    private static final String JSON_URL = "http://kezinking.com/SampleConLogin1"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     // etcheck=(EditText) findViewById(R.id.etcheck); 
     // txtCheck=(TextView) findViewById(R.id.txtCheck); 
     textViewJSON = (TextView) findViewById(R.id.textViewJSON); 
     textViewJSON.setMovementMethod(new ScrollingMovementMethod()); 
     buttonGet = (Button) findViewById(R.id.buttonGet); 
     // buttonParse = (Button) findViewById(R.id.buttonParse); 
     buttonGet.setOnClickListener(this); 
     // buttonParse.setOnClickListener(this); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    @Override 
    public void onClick(View v) { 
     if(v==buttonGet){ 
      getJSON(JSON_URL); 

     } 

     //if(v==buttonParse){ 
      // showParseActivity(); 
     //} 
    } 

    private void showParseActivity() { 
     Intent intent = new Intent(this, Showdata.class); 
     intent.putExtra(MY_JSON,textViewJSON.getText().toString()); 
     startActivity(intent); 
    } 


    private void getJSON(String url) { 
     class GetJSON extends AsyncTask<String, Void, String>{ 
      ProgressDialog loading; 

      @Override 
      protected void onPreExecute() { 
       super.onPreExecute(); 
       loading = ProgressDialog.show(MainActivity.this, "Please Wait...",null,true,true); 
      } 

      @Override 
      protected String doInBackground(String... params) { 

       String uri = params[0]; 

       BufferedReader bufferedReader = null; 
       try { 
        URL url = new URL(uri); 
        HttpURLConnection con = (HttpURLConnection) url.openConnection(); 
        StringBuilder sb = new StringBuilder(); 

        bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream())); 

        String json; 
        while((json = bufferedReader.readLine())!= null){ 
         sb.append(json+"\n"); 
        } 

        return sb.toString().trim(); 

       }catch(Exception e){ 
        return null; 
       } 

      } 

      @Override 
      protected void onPostExecute(String s) { 
       super.onPostExecute(s); 
       loading.dismiss(); 
       textViewJSON.setText(s); 
      } 
     } 
     GetJSON gj = new GetJSON(); 
     gj.execute(url); 
    } 
} 

機器人:-activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> 
    <EditText 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/etcheck"/> 


    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/button_get" 
     android:id="@+id/buttonGet" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:maxLines="20" 
     android:scrollbars="vertical" 
     android:id="@+id/textViewJSON" /> 
    <!-- <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/txtCheck"/> --> 
<!-- 
    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Parse JSON" 
     android:id="@+id/buttonParse" /> --> 


</LinearLayout> 

PHP文件級

<?php 
$con=mysqli_connect("localhost","amodbina0106","Amodbina200","kezin_king"); 
if ($con->connect_error) { 
    die("Connection failed: " . $con->connect_error); 
} 

?> 

<?php 
$sql=mysqli_query($con,"SELECT * FROM `Test` WHERE 1"); 
$result=array(); 
while($row=mysqli_fetch_assoc($sql)) 
{ 
    $result[]=$row; 
} 
echo json_encode(array("result"=>$result)); 
?> 

輸出的PHP: -

{"result":[{"Vendor_ID":"1","username":"nupur","password":"nupur"},{"Vendor_ID":"4","username":"","password":""},{"Vendor_ID":"3","username":"nupur","password":"1234"},{"Vendor_ID":"5","username":"RAJ","password":"RAJ"},{"Vendor_ID":"6","username":"RAJ","password":"RAJ"},{"Vendor_ID":"7","username":"","password":""},{"Vendor_ID":"8","username":"","password":""},{"Vendor_ID":"9","username":"","password":""},{"Vendor_ID":"10","username":"","password":""},{"Vendor_ID":"11","username":"","password":""},{"Vendor_ID":"12","username":"RAM","password":"RAM"},{"Vendor_ID":"13","username":"RAM","password":"RAM"},{"Vendor_ID":"14","username":"RAM","password":"RAM"},{"Vendor_ID":"15","username":"RAM","password":"RAM"},{"Vendor_ID":"16","username":"Nupur","password":"Nupur"},{"Vendor_ID":"17","username":"Nupur","password":"Nupur"},{"Vendor_ID":"18","username":"Nupur","password":"Nupur"},{"Vendor_ID":"19","username":"Nupur","password":"Nupur"},{"Vendor_ID":"20","username":"Rohit","password":"Rohit"},{"Vendor_ID":"21","username":"RAM","password":"RAM"},{"Vendor_ID":"22","username":"","password":""},{"Vendor_ID":"23","username":"","password":""},{"Vendor_ID":"24","username":"RAM","password":"RAM"},{"Vendor_ID":"35","username":"SAGAR","password":"SAGAR"},{"Vendor_ID":"34","username":"SAGAR","password":"SAGAR"},{"Vendor_ID":"33","username":"SAGAR","password":"SAGAR"},{"Vendor_ID":"32","username":"RAM","password":"RAM"},{"Vendor_ID":"31","username":"","password":""},{"Vendor_ID":"36","username":"SAGAR","password":"SAGAR"},{"Vendor_ID":"37","username":"SAGAR","password":"SAGAR"},{"Vendor_ID":"38","username":"SAGAR","password":"SAGAR"},{"Vendor_ID":"39","username":"SAGAR","password":"SAGAR"},{"Vendor_ID":"40","username":"SAGAR","password":"SAGAR"},{"Vendor_ID":"41","username":"SAGAR","password":"SAGAR"},{"Vendor_ID":"73","username":"","password":""},{"Vendor_ID":"43","username":"SAGAR","password":"SAGAR"},{"Vendor_ID":"44","username":"SAGAR","password":"SAGAR"},{"Vendor_ID":"45","username":"SAGAR","password":"SAGAR"},{"Vendor_ID":"46","username":"SAGAR","password":"SAGAR"},{"Vendor_ID":"47","username":"username","password":"password"},{"Vendor_ID":"74","username":"SAGAR","password":"SAGAR"},{"Vendor_ID":"75","username":"SAGAR","password":"SAGAR"},{"Vendor_ID":"72","username":"Krishna","password":"Krishna"},{"Vendor_ID":"68","username":"Honey","password":"Honey"},{"Vendor_ID":"69","username":"Ram","password":"Ram"},{"Vendor_ID":"70","username":"Ram","password":"Ram"},{"Vendor_ID":"71","username":"Ram","password":"Ram"}]} 

實施例: - 如果我提供Vendor_ID i n edittext as 1 它應該提供給我用戶名: - nupur 密碼: - 文本視圖中的nupur 。

回答

0

最簡單的方式存儲密碼,你可以使用JSON解析器像GSON。如果您想使用內置庫在Android(org.json),你可以使用類似這樣

String jsonString = "{\"result\":[{\"Vendor_ID\":\"1\",\"username\":\"nupur\",\"password\":\"nupur\"},{\"Vendor_ID\":\"4\",\"username\":\"\",\"password\":\"\"},{\"Vendor_ID\":\"3\",\"username\":\"nupur\",\"password\":\"1234\"},{\"Vendor_ID\":\"5\",\"username\":\"RAJ\",\"password\":\"RAJ\"},{\"Vendor_ID\":\"6\",\"username\":\"RAJ\",\"password\":\"RAJ\"},{\"Vendor_ID\":\"7\",\"username\":\"\",\"password\":\"\"},{\"Vendor_ID\":\"8\",\"username\":\"\",\"password\":\"\"},{\"Vendor_ID\":\"9\",\"username\":\"\",\"password\":\"\"},{\"Vendor_ID\":\"10\",\"username\":\"\",\"password\":\"\"},{\"Vendor_ID\":\"11\",\"username\":\"\",\"password\":\"\"},{\"Vendor_ID\":\"12\",\"username\":\"RAM\",\"password\":\"RAM\"},{\"Vendor_ID\":\"13\",\"username\":\"RAM\",\"password\":\"RAM\"},{\"Vendor_ID\":\"14\",\"username\":\"RAM\",\"password\":\"RAM\"},{\"Vendor_ID\":\"15\",\"username\":\"RAM\",\"password\":\"RAM\"},{\"Vendor_ID\":\"16\",\"username\":\"Nupur\",\"password\":\"Nupur\"},{\"Vendor_ID\":\"17\",\"username\":\"Nupur\",\"password\":\"Nupur\"},{\"Vendor_ID\":\"18\",\"username\":\"Nupur\",\"password\":\"Nupur\"},{\"Vendor_ID\":\"19\",\"username\":\"Nupur\",\"password\":\"Nupur\"},{\"Vendor_ID\":\"20\",\"username\":\"Rohit\",\"password\":\"Rohit\"},{\"Vendor_ID\":\"21\",\"username\":\"RAM\",\"password\":\"RAM\"},{\"Vendor_ID\":\"22\",\"username\":\"\",\"password\":\"\"},{\"Vendor_ID\":\"23\",\"username\":\"\",\"password\":\"\"},{\"Vendor_ID\":\"24\",\"username\":\"RAM\",\"password\":\"RAM\"},{\"Vendor_ID\":\"35\",\"username\":\"SAGAR\",\"password\":\"SAGAR\"},{\"Vendor_ID\":\"34\",\"username\":\"SAGAR\",\"password\":\"SAGAR\"},{\"Vendor_ID\":\"33\",\"username\":\"SAGAR\",\"password\":\"SAGAR\"},{\"Vendor_ID\":\"32\",\"username\":\"RAM\",\"password\":\"RAM\"},{\"Vendor_ID\":\"31\",\"username\":\"\",\"password\":\"\"},{\"Vendor_ID\":\"36\",\"username\":\"SAGAR\",\"password\":\"SAGAR\"},{\"Vendor_ID\":\"37\",\"username\":\"SAGAR\",\"password\":\"SAGAR\"},{\"Vendor_ID\":\"38\",\"username\":\"SAGAR\",\"password\":\"SAGAR\"},{\"Vendor_ID\":\"39\",\"username\":\"SAGAR\",\"password\":\"SAGAR\"},{\"Vendor_ID\":\"40\",\"username\":\"SAGAR\",\"password\":\"SAGAR\"},{\"Vendor_ID\":\"41\",\"username\":\"SAGAR\",\"password\":\"SAGAR\"},{\"Vendor_ID\":\"73\",\"username\":\"\",\"password\":\"\"},{\"Vendor_ID\":\"43\",\"username\":\"SAGAR\",\"password\":\"SAGAR\"},{\"Vendor_ID\":\"44\",\"username\":\"SAGAR\",\"password\":\"SAGAR\"},{\"Vendor_ID\":\"45\",\"username\":\"SAGAR\",\"password\":\"SAGAR\"},{\"Vendor_ID\":\"46\",\"username\":\"SAGAR\",\"password\":\"SAGAR\"},{\"Vendor_ID\":\"47\",\"username\":\"username\",\"password\":\"password\"},{\"Vendor_ID\":\"74\",\"username\":\"SAGAR\",\"password\":\"SAGAR\"},{\"Vendor_ID\":\"75\",\"username\":\"SAGAR\",\"password\":\"SAGAR\"},{\"Vendor_ID\":\"72\",\"username\":\"Krishna\",\"password\":\"Krishna\"},{\"Vendor_ID\":\"68\",\"username\":\"Honey\",\"password\":\"Honey\"},{\"Vendor_ID\":\"69\",\"username\":\"Ram\",\"password\":\"Ram\"},{\"Vendor_ID\":\"70\",\"username\":\"Ram\",\"password\":\"Ram\"},{\"Vendor_ID\":\"71\",\"username\":\"Ram\",\"password\":\"Ram\"}]}"; 
int idYouWantToSearch = Integer.parseInt(editTextThatHasIdYouWantToSearch.getText().toString()); 
String username = ""; 
String password = ""; 
try { 
    JSONObject jsonObject = new JSONObject(jsonString); 
    JSONArray result = jsonObject.getJSONArray("result"); 
    //iterate through json array and check if id is same with your search 
    for (int i = 0; i < result.length(); i++) { 
     JSONObject item = result.getJSONObject(i); 
     int id = item.getInt("Vendor_ID"); 
     if (id == idYouWantToSearch) { 
      username = item.getString("username"); 
      password = item.getString("password"); 
      break; 
     } 
    } 
} catch (JSONException e) { 
    e.printStackTrace(); 
} 
//here you can show the result 
Log.v(this.getClass().getSimpleName(), "username = " + username); 
Log.v(this.getClass().getSimpleName(), "password = " + password); 
+0

感謝您的最佳和最簡單的答案..! –

0

使用JSONObject類它會更容易。

首先檢索什麼在EDITTEXT有:

String token = editText.getText().toSting().trim();// token = Vendor_ID 

然後用你我會打電話給JSONString創建一個新的JSONObject

JSONObject jo = new JSONObject(JSONString); 

用的JSONObject你可以在Web服務器得到的迴應檢索任何值。查看docs以瞭解如何檢索不同類型的數據,但現在我假設您檢索的數據是字符串。

String userName = JSONObject.getString(token); 

與此userName shoulb是 「nupur」

P.S這是不好的做法,以純文本

0

考慮到我們在這裏(安卓)談論客戶端我會建議使用Retrofit2或排球,這些庫允許你更好地管理你的網絡,爲你處理後臺線程(並且已知比AsyncTask更好,你可以谷歌它),並且關於JSON解析 - 使用庫也是很好的,比如GSON,或者其他的東西在那裏。

所以你有什麼是這樣的:

  1. 找一個庫聯網。
  2. 獲取JSON解析庫。
  3. 將兩者集成在一起(創建包裝,接口,回調等)

改造有一個選項可以直接添加轉換器,因此可以節省您的一個不錯的一部分解析自己(除非你需要在分析過程中更多的靈活性,在這種情況下,你可以尋找更多的幫助,在這裏堆棧溢出

現在,關於解析 - 。正確的方法去它會是─

  1. 解析您收到JSON對象
  2. 用它來發送另一個請求到服務器

如 - 請求與用戶名是由客戶端收到 - >分析 - >用戶完成密碼 - >信息被髮送回再次

一些東西,可能是有用的:

Volley Example

Retrofit Example for custom requests

希望這會有所幫助,無論如何 - 閱讀有關REST API消費主題的更多信息。