【发布时间】:2016-08-06 07:14:37
【问题描述】:
嘿,我在 000webhost 上有某些数据,我想将其发送到我的 android studio 应用程序。我已经通过直接在 000webhost 上运行来检查查询,它正在工作。
但它没有向 android studio 发送数据。我尝试打印它打印 null 的值。基本上我发送的数据试图发送一个数组。我的功能是在按钮单击时执行的。我希望一个标记出现在纬度经度值上从 php.BUt 发送它在 logcat 上发送 null 而没有错误。
apiconnector 我正在使用
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;
}
}
/*****函数,我在哪里调用连接器*****/
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();
}
}
}
/**** 分配给按钮的功能 ***/
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 时要小心,因为会发生多个功能。我只检查一个按钮单击,它具有 onthrow 函数作为 onclick intialized。检查 json 数组在 logcat 中打印为 null 的位置。
04-14 21:16:51.387 1109-1109/? D/PhoneStatusBar: removeNotification key=android.os.BinderProxy@42b5f3f0 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
【问题讨论】:
-
在 PHP 中尝试将
print替换为echo。此外,在 while 循环之外创建$output数组。目前它的范围仅限于 while 块。 -
用 echo 试过还是一样的结果
-
JSONObject json = jsonArray.getJSONObject(0); cn 你告诉我是 thr ny prblm 在 callemptybin 中有这条线
标签: java php android arrays json