我剛剛開始使用LoopJ AndroidAsyncHttp庫及其輝煌的數據上傳。使用LoopJ AndroidAsyncHttp檢索值
但是,我現在試圖使用get請求來獲得響應,我似乎無法理解爲什麼我的onSuccess和我的onFailure方法都沒有被調用。我查看了這裏的問題,似乎無法找到解決onSuccess方法的新實現的問題。有人可以幫忙嗎?
正在呼籲ButtonClick方法:
public void displayUploaded(View view){
RequestParams params=new RequestParams();
try{
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://192.168.1.4/clientservertest/returnUploadedImages.php",
new JsonHttpResponseHandler() {
@Override
public void onSuccess(JSONObject jsonObject) {
// Display a "Toast" message
Toast.makeText(getApplicationContext(), "Success!", Toast.LENGTH_LONG).show();
Log.d("android", jsonObject.toString());
}
@Override
public void onFailure(int statusCode, Throwable throwable, JSONObject error) {
// Display a "Toast" message
Toast.makeText(getApplicationContext(), "Error: " + statusCode + " " + throwable.getMessage(), Toast.LENGTH_LONG).show();
// Log error message
// to help solve any problems
Log.e("android", statusCode + " " + throwable.getMessage());
}
});
}
catch(Exception e){
Toast toast2=Toast.makeText(getApplicationContext(),"Failed first TRY",Toast.LENGTH_LONG);
toast2.show();
e.printStackTrace();
}
}
這是我的PHP代碼(使用鉻郵差客戶端,它工作得很好,我已經張貼在EDIT2輸出):
<?php
#Connect to Database
$con = mysqli_connect("localhost","root","", "mytestdatabase");
#Check connection
if (mysqli_connect_errno()) {
echo 'Database connection error: ' . mysqli_connect_error();
exit();
}
#Query the database to get the user details.
$userdetails = mysqli_query($con, "SELECT * FROM images");
#If no data was returned, check for any SQL errors
if (!$userdetails) {
echo 'Could not run query: ' . mysqli_error($con);
exit;
}
#Return the results
$rows = array();
while($r = mysqli_fetch_assoc($userdetails)) {
$rows[] = $r;
}
print(json_encode($rows));
?>
我我也嘗試了使用其他ResponseHandlers的同樣的事情,但那也行不通。真的希望得到答案!
編輯:添加有效和在同一活動中完美工作的代碼。這是一個發佈請求:
File selectedPicture=new File(picturePath);
RequestParams params=new RequestParams();
try{
params.put("UploadedPic",selectedPicture);
AsyncHttpClient client = new AsyncHttpClient();
client.post("http://192.168.1.4/clientservertest/imageupload.php", params,new AsyncHttpResponseHandler());
}
EDIT2:
[{ 「ID」: 「7」, 「路徑」:「上傳/ speed.png的圖像響應從PHP頁面返回給郵遞員客戶「},{」 ID 「:」 8" , 「路徑」: 「上傳/ Untitled.png」},{ 「ID」: 「9」, 「路徑」: 「上傳/ Untitled.png」},{「編碼「:」10「,」path「:」uploadsspeed_2.png「},{」id「:」11「,」path「:」uploads/speed_3.png「}]
您是否能夠從運行在Android設備上的瀏覽器訪問URL? –
用這個http://192.168.1.4/clientservertest/returnUploadedImages.php更改你的網址到localhost://192.168.1.4/clientservertest/returnUploadedImages.php – Amardeepvijay
@ MattO,@ Amardeep我懷疑這是一個URL問題,因爲類似的代碼是在同一項活動中爲我工作。看到我的編輯的工作代碼。 – RohanC