- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嘿,我在 000webhost 上有一些数据,我想将其发送到我的 android studio 应用程序。我已经通过直接在 000webhost 上运行来检查查询,它正在运行。
但它没有向 android studio 发送数据。我尝试打印它打印 null 的值。基本上我发送的数据试图发送一个数组。我的功能是在单击按钮时执行的。我希望标记出现在纬度经度值上从 php 发送。但是它在 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 结果。请小心使用 logcat,因为会发生多个函数。我只检查一个按钮单击,它具有 onclick 初始化的 onthrow 功能。检查 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
最佳答案
帖子太长,stackoverflow 不喜欢它们。我有一些建议,您可以自己尝试。
使用 Rest Console 测试服务器的预期响应。 ( Link for Google Chrome )
忘记 DefaultHttpClient
,使用 Volley
,欢迎来到现代世界!
保持简短。没有人关心 onthrow()
做了什么。
要将 JSON 解析为 Java 对象,请使用 Gson
.
使用 Volley
意味着您将不再解析 JSON,不再需要创建 AsyncTasks,也不再需要为 doInBackground()
编写代码。我做了一个GitHub gist在 Android 代码中实现 Volley
。
关于java - 将数组从 php 发送到 android studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36628092/
我最近在/ drawable中添加了一些.gifs,以便可以将它们与按钮一起使用。这个工作正常(没有错误)。现在,当我重建/运行我的应用程序时,出现以下错误: Error: Gradle: Execu
Android 中有返回内部存储数据路径的方法吗? 我有 2 部 Android 智能手机(Samsung s2 和 s7 edge),我在其中安装了一个应用程序。我想使用位于这条路径中的 sqlit
这个问题在这里已经有了答案: What's the difference between "?android:" and "@android:" in an android layout xml f
我只想知道 android 开发手机、android 普通手机和 android root 手机之间的实际区别。 我们不能从实体店或除 android marketplace 以外的其他地方购买开发手
自Gradle更新以来,我正在努力使这个项目达到标准。这是一个团队项目,它使用的是android-apt插件。我已经进行了必要的语法更改(编译->实现和apt->注释处理器),但是编译器仍在告诉我存在
我是android和kotlin的新手,所以请原谅要解决的一个非常简单的问题! 我已经使用导航体系结构组件创建了一个基本应用程序,使用了底部的导航栏和三个导航选项。每个导航选项都指向一个专用片段,该片
我目前正在使用 Facebook official SDK for Android . 我现在正在使用高级示例应用程序,但我不知道如何让它获取应用程序墙/流/状态而不是登录的用户。 这可能吗?在那种情
我在下载文件时遇到问题, 我可以在模拟器中下载文件,但无法在手机上使用。我已经定义了上网和写入 SD 卡的权限。 我在服务器上有一个 doc 文件,如果用户单击下载。它下载文件。这在模拟器中工作正常但
这个问题在这里已经有了答案: What is the difference between gravity and layout_gravity in Android? (22 个答案) 关闭 9
任何人都可以告诉我什么是 android 缓存和应用程序缓存,因为当我们谈论缓存清理应用程序时,它的作用是,缓存清理概念是清理应用程序缓存还是像内存管理一样主存储、RAM、缓存是不同的并且据我所知,缓
假设应用程序 Foo 和 Eggs 在同一台 Android 设备上。任一应用程序都可以获取设备上所有应用程序的列表。一个应用程序是否有可能知道另一个应用程序是否已经运行以及运行了多长时间? 最佳答案
我有点困惑,我只看到了从 android 到 pc 或者从 android 到 pc 的例子。我需要制作一个从两部手机 (android) 连接的 android 应用程序进行视频聊天。我在想,我知道
用于使用 Android 以编程方式锁定屏幕。我从 Stackoverflow 之前关于此的问题中得到了一些好主意,并且我做得很好,但是当我运行该代码时,没有异常和错误。而且,屏幕没有锁定。请在这段代
文档说: android:layout_alignParentStart If true, makes the start edge of this view match the start edge
我不知道这两个属性和高度之间的区别。 以一个TextView为例,如果我将它的layout_width设置为wrap_content,并将它的width设置为50 dip,会发生什么情况? 最佳答案
这两个属性有什么关系?如果我有 android:noHistory="true",那么有 android:finishOnTaskLaunch="true" 有什么意义吗? 最佳答案 假设您的应用中有
我是新手,正在尝试理解以下 XML 代码: 查看 developer.android.com 上的文档,它说“starStyle”是 R.attr 中的常量, public static final
在下面的代码中,为什么当我设置时单选按钮的外观会发生变化 android:layout_width="fill_parent" 和 android:width="fill_parent" 我说的是
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
假设我有一个函数 fun myFunction(name:String, email:String){},当我调用这个函数时 myFunction('Ali', 'ali@test.com ') 如何
我是一名优秀的程序员,十分优秀!