2015-12-31 92 views
1

我是Android新手。我正在製作一個應用程序,使用凌雲庫從服務器獲取數據。但是當我們使用id或參數時,我不知道如何從服務器獲取數據。請幫忙。使用參數從服務器解析Json數據

public class MainActivity extends AppCompatActivity { 

    Button btn_next,btn_search,cross; 
    TextView title; 
    EditText et; 

    private ProgressDialog pDialog; 

    public static final String JSON_URL = "http://bcshymns.com/heading.php"; 

    private Button buttonGet; 

    private ListView listView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    cross=(Button)findViewById(R.id.cross); 
     et=(EditText)findViewById(R.id.search); 
     btn_next=(Button)findViewById(R.id.btnnext); 
     btn_search=(Button)findViewById(R.id.btnsearch); 
     title=(TextView)findViewById(R.id.tv_title); 
     btn_search.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       cross.setVisibility(View.VISIBLE); 
       title.setVisibility(View.INVISIBLE); 
       et.setVisibility(View.VISIBLE); 
      } 
     }); 
     cross.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       title.setVisibility(View.INVISIBLE); 
       btn_search.setVisibility(View.VISIBLE); 
      } 
     }); 
     btn_next.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent i = new Intent(MainActivity.this, Subheading.class); 
       startActivity(i); 
      } 
     }); 



     listView = (ListView) findViewById(R.id.listView); 
     sendRequest(); 

     pDialog = new ProgressDialog(this); 
     // Showing progress dialog before making http request 
     pDialog.setMessage("Loading..."); 
     pDialog.show(); 


     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       // String product = ((TextView) view).getText().toString(); 
       Intent i=new Intent(getApplicationContext(),Subcontent_main.class); 
       startActivity(i); 
      } 
     }); 
    } 

    private void sendRequest(){ 

     StringRequest stringRequest = new StringRequest(JSON_URL, 
       new Response.Listener<String>() { 
        @Override 
        public void onResponse(String response) { 
         showJSON(response); 
        } 
       }, 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         Toast.makeText(MainActivity.this,error.getMessage(),Toast.LENGTH_LONG).show(); 

        } 
       }); 

     RequestQueue requestQueue = Volley.newRequestQueue(this); 
     requestQueue.add(stringRequest); 

    } 

    private void showJSON(String json){ 
     ParseJSON_Heading pj = new ParseJSON_Heading(json); 
     pj.parseJSON(); 
     CustomList cl = new CustomList(this, ParseJSON_Heading.heading,ParseJSON_Heading.from,ParseJSON_Heading.to); 
     listView.setAdapter(cl); 
     hidePDialog(); 
    } 


    private void hidePDialog() { 
     if (pDialog != null) { 
      pDialog.dismiss(); 
      pDialog = null; 
     } 
    } 

}` 


public class ParseJSON_Heading { 
    public static String[] heading; 
    public static String[] from; 
    public static String[] to; 
    public static String[] id; 


    public static final String JSON_ARRAY_SUBHEADING = "tbl_heading"; 
    public static final String JSON_ARRAY_HEADING = "tbl_heading"; 
    public static final String KEY__HEADING_ID = "intheadingId"; 
    public static final String KEY_TEXT_HEADING = "vchheading"; 
    public static final String KEY_TEXT_FROM = "vchfrom"; 
    public static final String KEY_TEXT_TO = "vchto"; 

    private JSONArray users = null; 

    private String json; 

    public ParseJSON_Heading(String json){ 
     this.json = json; 
    } 

    protected void parseJSON(){ 
     JSONObject jsonObject=null; 
     try { 
      jsonObject = new JSONObject(json); 
      users = jsonObject.getJSONArray(JSON_ARRAY_HEADING); 
      id=new String[users.length()]; 
      heading = new String[users.length()]; 
      from = new String[users.length()]; 
      to = new String[users.length()]; 

      for(int i=0;i<users.length();i++){ 
       JSONObject jo = users.getJSONObject(i); 
       id[i]=jo.getString(KEY__HEADING_ID); 
       heading[i] = jo.getString(KEY_TEXT_HEADING); 
       from[i] = jo.getString(KEY_TEXT_FROM); 
       to[i] = jo.getString(KEY_TEXT_TO); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 
} 
+0

調查GSON。 IT是將JSON解析爲對象的最佳選擇。 –

+0

要稍微清楚一點,您是否試圖通過從您的應用程序發送特定的ID作爲參數從服務器獲取某些內容? –

+0

你想發佈一些參數嗎? –

回答

0

如果您需要提交請求參數,那麼你就必須重寫()將鍵值格式發送應返回的參數列表方法getParams。

在下面的代碼中,我們提交名稱,電子郵件和密碼作爲請求參數。

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST, 
      url, null, 
      new Response.Listener<JSONObject>() { 

       @Override 
       public void onResponse(JSONObject response) { 
        Log.d(TAG, response.toString()); 
        pDialog.hide(); 
       } 
      }, new Response.ErrorListener() { 

       @Override 
       public void onErrorResponse(VolleyError error) { 
        VolleyLog.d(TAG, "Error: " + error.getMessage()); 
        pDialog.hide(); 
       } 
      }) { 

     @Override 
     protected Map<String, String> getParams() { 
      Map<String, String> params = new HashMap<String, String>(); 
      params.put("name", "foo"); 
      params.put("email", "[email protected]"); 
      params.put("password", "foobar"); 

      return params; 
     } 

    };