2016-04-14 32 views
1

嘿,我有我想要發送到我的android工作室應用程序000webhost某些數據。我通過直接在000webhost上運行來檢查查詢,它正在工作。發送陣列從PHP到Android工作室

但它沒有發送數據到android studio.I嘗試打印它打印的值null.Basically數據im發送試圖發送一個數組。我的功能是在按鈕單擊執行。我想要一個標記出現在緯度經度值從php.But發送null,沒有錯誤logcat。

使用

package com.example.arpanagarwal.sba; 

import android.util.Log; 
import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.ParseException; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.util.EntityUtils; 
import org.json.JSONArray; 
import org.json.JSONException; 

import java.io.IOException; 

public class Apiconnector2 { 

public JSONArray getUser() { 
    //int i = 1; 


    String url; 
    //url = "http://10.0.0.2/uservalidate.php?BookName="+temp; 
    HttpEntity httpEntity = null; 
    //ArrayList<NameValuePair> nameValuePairs1 = new      ArrayList<NameValuePair>(); 
    //nameValuePairs1.add(new BasicNameValuePair("search","sys")); 
    try { 
     System.out.println("step 3"); 
     url = "http://askamit2012.netne.net/callemptybin.php"; 
     DefaultHttpClient httpclient = new DefaultHttpClient(); 
     HttpGet httpGet = new HttpGet(url); 
     //httpGet.setEntity(new UrlEncodedFormEntity(nameValuePairs1)); 

     HttpResponse httpResponse = httpclient.execute(httpGet); 
     httpEntity = httpResponse.getEntity(); 

    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    JSONArray jsonArray = null; 

    if (httpEntity != null) { 
     try { 
      String entityResponse = EntityUtils.toString(httpEntity); 
      // String entityResponse = null; 
      Log.e("Entity Response :", entityResponse); 
      jsonArray = new JSONArray(entityResponse); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } catch (ParseException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
    return jsonArray; 
} 

}

/*****機能的研究,其中IM呼叫連接器*****/

public class callemptybin extends AsyncTask<Apiconnector2,Long,JSONArray> 
{ 
    protected JSONArray doInBackground(Apiconnector2... params) { 
     System.out.println("step1"); 
     return params[0].getUser(); 

    } 
    @Override 
    protected void onPreExecute() 
    { 
     System.out.println("step4"); 
     super.onPreExecute(); 
    } 

    protected void onPostExecute(JSONArray jsonArray) 
    { 
     System.out.println("jsonarray=" + jsonArray); 
     try{ 
      if(jsonArray==null) 
      { 
       System.out.println("null"); 
      } 
      else { 
       JSONObject json = jsonArray.getJSONObject(0); 

       double lat1 = json.getDouble("latitude"); 
       double long1 = json.getDouble("longitude"); 

       LatLng jpnagar = new LatLng(lat1, long1); 
       mMap.addMarker(new MarkerOptions().position(jpnagar).title("Bin Empty").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))); 
      } 
     } 
     catch (JSONException e) { 
      System.out.println("catch"); 
      e.printStackTrace(); 
     } 
    } 
} 

/****功能apiconnector IM其被分配到按鈕***/

public void onthrow(View view) 
{ 
    if(view.getId()==R.id.button3) 
    { 
     //throwtrash(); 
     System.out.println("step 00"); 
     new callemptybin().execute(new Apiconnector2()); 
     tt=1; 
    } 
} 

/***** PHP腳本****/

<?php 
if (isset($_SERVER['HTTP_ORIGIN'])) 
{ 
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}"); 
header('Access-Control-Allow-Credentials: true'); 
header('Access-Control-Max-Age: 86400'); // cache for 1 day 
} 

// Access-Control headers are received during OPTIONS requests 
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') 
{ 

if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'])) 
    header("Access-Control-Allow-Methods: GET, POST, OPTIONS");   

if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) 
    header("Access-Control-Allow-Headers:     {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}"); 

exit(0); 
} 


    $con = mysql_connect("mysql8.000webhost.com","********","*******"); 
    if(!$con) 
    { 
    die("Could not connect :".mysql_error()); 
    } 
    mysql_select_db("*********",$con); 


    //mysql_query($sql); 
    mysql_query("set names 'utf8'"); 
    $result=mysql_query("select * from GPS where marker=1"); 
    while($row=mysql_fetch_assoc($result)) 
    { 
    $output[]=$row; 
    } 
print(json_encode($output)); 

    mysql_close($con); 

?>  

logcat的result.plz小心logcat的,因爲THR r多個功能發生。我檢查只有一個按鈕單擊它具有onthrow函數onclick intialized.check其中json數組在logcat中打印爲null。

04-14 21:16:51.387 1109-1109/? D/PhoneStatusBar: removeNotification  [email protected] keyCode=1119220720 old=StatusBarNotification(pkg=com.android.systemui user=UserHandle{0} id=252119 tag=null score=10: Notification(pri=1 contentView=com.android.systemui/0x1090064 vibrate=null sound=null defaults=0x0 flags=0x2 kind=[null])) 
04-14 21:17:38.757 6128-6128/com.example.arpanagarwal.sba I/System.out: **jsonarray=null** 
04-14 21:17:38.767 6128-6128/com.example.arpanagarwal.sba I/System.out: null 
04-14 21:18:56.327 6819-6835/? D/AlertSyncService: onHandleIntent action = null 
+0

在PHP中嘗試用'echo'替換'print'。另外,在while循環之外創建'$ output'數組。目前它的範圍僅限於while塊。 –

+0

用echo回覆仍然是一樣的結果 –

+0

JSONObject json = jsonArray.getJSONObject(0);你告訴我,這是在紐約prblm這行callemptybin –

回答

1

該帖子太長,並且stackoverflow不喜歡它們。我有幾點建議你可以嘗試一下。

  • 使用Rest Console來測試服務器的預期響應。 (Link for Google Chrome

  • 忘記DefaultHttpClient,使用Volley,歡迎來到現代世界!

  • 保持簡短。沒有人關心onthrow()做什麼。

  • 要將JSON解析爲Java對象,請使用Gson

使用Volley意味着你將不再解析JSON,你不再需要創建AsyncTasks,不再寫代碼doInBackground()。我已經做了一個GitHub gist在android代碼中實現Volley

+0

你有問題的答案? –

+0

@arpanagarwal你提到它在你的瀏覽器上工作正常。你的問題可能是在連接上:一個意外的響應來到你的android應用程序或你的應用程序無法解析它。首先,通過在上面提供的鏈接中使用Rest Console,嘗試查找android應用程序是否正確接收數據。在你指出錯誤之後,我可以幫你。 –

+0

另外,在android代碼中,只要你願意,你可以「斷言(條件)」。使用它們來跟蹤任何意外的代碼流。最重要的是,使用android調試器。 –