public class Config {
public static final String DATA_URL = "http://192.168.1.2/retrieveone.php?id=";
public static final String KEY_NAME = "BusinessName";
public static final String KEY_AmountTotal = "AmountTotal";
public static final String KEY_RT = "RequiredTotal";
public static final String KEY_MT = "MaxTotal";
public static final String JSON_ARRAY = "result";
}
これは私のクラスファイルです。私のデータはなぜ出てこないのですか?
private void getData(){
String id = TempItem.toString().trim();
if (id.equals("")) {
Toast.makeText(this, "Please enter an id", Toast.LENGTH_LONG).show();
return;
}
String url = Config.DATA_URL+TempItem.toString().trim();
StringRequest stringRequest = new StringRequest(Request.Method.GET,url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
showJSON(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(KPDetails.this,error.toString(),Toast.LENGTH_LONG).show();
error.printStackTrace();
}
});
int socketTimeout =50000;//5 seconds - change to what you want
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
stringRequest.setRetryPolicy(policy);
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void showJSON(String response){
String name="";
String AmountTotal="";
String RequiredTotal = "";
String MaxTotal = "";
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
JSONObject stallsData = result.getJSONObject(0);
name = stallsData.getString(Config.KEY_NAME);
AmountTotal = stallsData.getString(Config.KEY_AmountTotal);
MaxTotal = stallsData.getString(Config.KEY_MT);
RequiredTotal = stallsData.getString(Config.KEY_RT);
} catch (JSONException e) {
e.printStackTrace();
}
Stall.setText("Name:\t"+name+"\nAmountTotal:\t" +AmountTotal+ "\nMaxTotal:\t"+ MaxTotal);
}
これは私のコンストラクタであり、mysqlデータベースからデータを取得します。しかし、私はsetTextを取得することができましたが、configの文字列の値は取得できませんでした。
これは私のPHPファイルです。
?php
if($_SERVER['REQUEST_METHOD']=='GET'){
$id = $_GET['id'];
require_once('conn.php');
$sql = "SELECT * FROM business WHERE BusinessID='".$id."'";
$r = mysqli_query($conn,$sql);
$res = mysqli_fetch_array($r);
$result = array();
array_push($result,array(
"BusinessName"=>$res['BusinessName'],
"AmountTotal"=>$res['AmountTotal'],
"RequiredTotal"=>$res['RequiredTotal'],
"MaxTotal"=>$res['MaxTotal']
)
);
echo json_encode(array("result"=>$result));
mysqli_close($conn);
}
"http://localhost/retrieveone.php?id=1"は完全に正常に動作しますが、値は自分のsetTextに表示されません。 私はAndroid Studioの初心者です。どんな助けもありがとうございます。前もって感謝します。
編集:
これは私のエラーです。 java.lang.String型の値接続はJSONObjectに変換できません
JSONObjectの出力を表示できますか? –
@ Mahdi.Pishguyあなたは間違った人をテジャーしました。私は質問を投稿した人ではありません:) –
@HahaHeHeあなたの解析済みjsonが正しくありません。Log.e( "error"、e.getMessage()) 'の後に' e .printStackTrace(); 'Logcatで何が起こるかを確認するためにappを再実行します。 –