Hallo versuche ich die folgenden JSON-Daten auf meinem Android zu bekommen. Ein Fall-Link kann http://digitaresolutions.com/apps/drugindex/getbrand.php?id=bactiflox sein. Kann mir jemand helfen? Hinweis: Das Array hat ein einzelnes Objekt.Get JSON Daten auf meinem Android
public class Search_view extends AppCompatActivity {
String brandURL = "http://digitaresolutions.com/apps/drugindex/getbrand.php?id=";
TextView txtProduct;
String product;
String url="";
TextView outputText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.brand_item);
txtProduct = (TextView) findViewById(R.id.brand_label);
// getting attached intent data
Intent i = getIntent();
product = i.getStringExtra("product");
// displaying selected product name
url = brandURL + product;
txtProduct.setText("Drug Molecule: " + product + "\n Product Url: " + url);
new JSONParse().execute();
}
// JSON Parsing
private class JSONParse extends AsyncTask<String, Void, String> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
outputText = (TextView) findViewById(R.id.drug_info);
pDialog = new ProgressDialog(Search_view.this);
pDialog.setMessage("Loading Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
outputText.setText("Getting Brand Please wait..");
}
@Override
protected String doInBackground(String... args) {
StringBuilder sb = new StringBuilder();
String content = MyHttpUrlConnection.getData(url);
try {
JSONObject obj = new JSONObject(content);
JSONArray drug = obj.getJSONArray("result");
for (int i = 0; i < drug.length(); i++) {
JSONObject p = (JSONObject) drug.get(i);
String drg = (String) drug.get(i);
Log.i("Drug Info", drg);
sb.append(i + 1 + " Manufacturer : " + p.getString("manu") + "\n");
sb.append("Drug Info : " + p.getString("details") + "\n");
}
return sb.toString();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
pDialog.dismiss();
outputText.setText(result);
}
}
// End JSON JSON Parsing
}
Der folgende Code ist mein Http Url
Anschluss:
public class MyHttpUrlConnection {
public static String getData(String uri) {
BufferedReader reader = null;
try {
URL url = new URL(uri);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setReadTimeout(10000);
con.setConnectTimeout(15000);
con.setDoInput(true);
con.connect();
InputStream is = con.getInputStream();
reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String data = null;
String webPage = "";
while ((data = reader.readLine()) != null) {
webPage += data + "\n";
}
return webPage;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
}
}
ist dies eine Anfrage erhalten oder senden? –
Bitte werfen Sie einen Blick auf Volley: http://developer.android.com/training/volley/index.html – Machado
Es ist eine Anfrage erhalten. Ich verwende einen Intent-Wert aus einer vorherigen Aktivität, um die URL abzuschließen: "http://digitraresolutions.com/apps/drugindex/getbrand.php?id=" sowie – Brian