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();
}
}
}
調查GSON。 IT是將JSON解析爲對象的最佳選擇。 –
要稍微清楚一點,您是否試圖通過從您的應用程序發送特定的ID作爲參數從服務器獲取某些內容? –
你想發佈一些參數嗎? –