- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试使用 http post 请求将 Android 用户的位置(纬度、经度)发送到 MySQL 数据库,但在运行程序时出现以下错误:
错误:
E/Buffer Error: Error converting result java.lang.NullPointerException
E/JSON Parser: Error parsing data org.json.JSONException: End of input at character 0 of
Logcat:
Logcat after changing "localhost" to my IP
Logcat after changing "localhost" to my IP
05-15 21:03:52.905 28777-28777/? E/Zygote: isWhitelistProcess - Process is Whitelisted
05-15 21:03:52.906 28777-28777/? E/libpersona: scanKnoxPersonas
Couldn't open the File - /data/system/users/0/personalist.xml - No such file or directory
Main_Activity:
import android.os.StrictMode;
import android.util.Log;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import android.Manifest;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import com.indooratlas.android.sdk.IALocation;
import com.indooratlas.android.sdk.IALocationListener;
import com.indooratlas.android.sdk.IALocationManager;
import com.indooratlas.android.sdk.IALocationRequest;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
JSONParser jsonParser = new JSONParser();
IALocationManager mLocationManager;
IALocationListener mLocationListener = new IALocationListener() {
@Override
public void onLocationChanged(IALocation iaLocation) {
TextView txtLoc = (TextView) findViewById(R.id.textView);
txtLoc.setText(String.valueOf(iaLocation.getLongitude() + ", " +
iaLocation.getLatitude()));
// build HTTP POST request to send these data to the database
List<NameValuePair> params = new ArrayList<NameValuePair>();
String deviceId = "1";
params.add(new BasicNameValuePair("deviceId", deviceId));
System.out.println("DEVICE ID " + deviceId);
params.add(new BasicNameValuePair("latitude",
Double.toString(iaLocation.getLongitude())));
System.out.println("Latitude + " + iaLocation.getLongitude());
params.add(new BasicNameValuePair("longitude",
Double.toString(iaLocation.getLatitude())));
System.out.println("Longitude+ " + iaLocation.getLatitude());
jsonParser.makeHttpRequest("http://localhost/android_connect/update11.php",
"POST", params);
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
};
private final int CODE_PERMISSIONS = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView txtLoc = (TextView) findViewById(R.id.textView);
mLocationManager = IALocationManager.create(this);
String[] neededPermissions = {
Manifest.permission.CHANGE_WIFI_STATE,
Manifest.permission.ACCESS_WIFI_STATE,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.BLUETOOTH,
Manifest.permission.BLUETOOTH_ADMIN
};
ActivityCompat.requestPermissions(this, neededPermissions,
CODE_PERMISSIONS);
StrictMode.ThreadPolicy policy = new
StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
public void onRequestPermissionsResult(int requestCode, String[]
permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions,
grantResults);
//Handle if any of the permissions are denied, in grantResults
}
protected void onResume() {
super.onResume();
mLocationManager.requestLocationUpdates(IALocationRequest.create(),
mLocationListener);
}
protected void onPause() {
mLocationManager.removeLocationUpdates(mLocationListener);
super.onPause();
}
protected void onDestroy() {
mLocationManager.destroy();
super.onDestroy();
}
}
Android_Manifest:
<?xml version="1.0" encoding="utf-8"?>
<uses-feature android:name="android.hardware.sensor.accelerometer"
android:required="true" />
<uses-feature android:name="android.hardware.sensor.compass"
android:required="true" />
<uses-feature android:name="android.hardware.sensor.gyroscope"
android:required="true" />
<uses-feature android:name="android.hardware.wifi"
android:required="true" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="IndoorAtlasExample"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.indooratlas.android.sdk.API_KEY"
android:value="<API key here>"/>
<meta-data android:name="com.indooratlas.android.sdk.API_SECRET"
android:value="<API secret here>"/>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
JSONParser:
import android.util.Log;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;
/**
* Used to submit new IndoorAtlas readings to the MySQL server via JSON on
the phone (this
* class) & PHP on the Web server.
*/
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST or GET mehtod
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
用于更新和插入MySQL数据库的php代码:
<?php
/*
All product details are read from HTTP Post Request
*/
// error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE);
// error_reporting(E_ERROR | E_PARSE);
if ($_SERVER ["REQUEST_METHOD"]=="POST"){
require'connectiontest.php';
createStudent();
}
// array for JSON response
$response = array();
function createstudent()
{
// check for required fields
if (isset($_POST['deviceId']) && isset($_POST['latitude']) &&
isset($_POST['longitude'])) {
// extract data from POST into variables
global $connect;
$deviceId = $_POST['deviceId'];
$latitude = $_POST['latitude'];
$longitude = $_POST['longitude'];
// $time = $_POST['time'];
$query = "SELECT * FROM cord WHERE deviceId = '$deviceId';";
$result=mysqli_query($connect,$query);
$count=mysqli_num_rows($result);
if($count>0)
{error_log("Database already has that deviceId, doing an UPDATE.", 0);
$query = "UPDATE cord SET latitude = '$latitude', longitude =
'$longitude' WHERE deviceId = '$deviceId';";
}
else{
error_log("Database is empty, doing an INSERT.", 0);
$query = "INSERT INTO cord (deviceId, latitude, longitude) VALUES
('$deviceId', '$latitude', '$longitude');";
}
mysqli_query($connect, $query) or die (mysqli_error($connect));
mysqli_close($connect);
// check if row inserted or not
if ($query) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Entry successfully inserted or updated.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Entry was not successfully inserted or
updated.";
// echoing JSON response
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
}
?>
最佳答案
如果您的服务器仍在本地,您应该使用 ip 地址而不是 localhost例如:
jsonParser.makeHttpRequest("http://192.168.1.4/android_connect/update11.php",
"POST", params);
}
注意:将192.168.1.4更改为您的本地IP地址,并确保您的手机和电脑连接到同一网络。
关于java - org.apache.http.conn.HttpHostConnectException : Connection to http://localhost refused,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50358449/
在流处理方面,Apache Beam和Apache Kafka之间有什么区别? 我也试图掌握技术和程序上的差异。 请通过您的经验报告来帮助我理解。 最佳答案 Beam是一种API,它以一种统一的方式使
有点n00b的问题。 如果我使用 Apache Ignite 进行消息传递和事件处理,是否还需要使用 Kafka? 与 Ignite 相比,Kafka 基本上会给我哪些(如果有的话)额外功能? 提前致
Apache MetaModel 是一个数据访问框架,它为发现、探索和查询不同类型的数据源提供了一个通用接口(interface)。 Apache Drill 是一种无架构的 SQL 查询引擎,它通过
Tomcat是一个广泛使用的java web服务器,而Apache也是一个web服务器,它们在实际项目使用中有什么不同? 经过一些研究,我有了一个简单的想法,比如, Apache Tomcat Ja
既然简单地使用 Apache 就足以运行许多 Web 应用程序,那么人们何时以及为什么除了 Apache 之外还使用 Tomcat? 最佳答案 Apache Tomcat是一个网络服务器和 Java
我在某个 VPS( friend 的带 cPanel 的 apache 服务器)上有一个帐户,我在那里有一个 public_html 目录。我们有大约 5-6 个网站: /home/myusernam
我目前正在尝试将模块加载到 Apache,使用 cmake 构建。该模块称为 mod_mapcache。它已成功构建并正确安装在/usr/lib/apache2/modules directroy 中
我对 url 中的问号有疑问。 例如:我有 url test.com/controller/action/part_1%3Fpart_2 (其中 %3F 是 url 编码的问号),并使用此重写规则:R
在同一台机器上,Apache 在端口 80 上运行,Tomcat 在端口 8080 上运行。 Apache 包括 html;css;js;文件并调用 tomcat 服务。 基本上 exampledom
Apache 1 和 Apache 2 的分支有什么区别? 使用一种或另一种的优点和缺点? 似乎 Apache 2 的缺点之一是使用大量内存,但也许它处理请求的速度更快? 最有趣的是 Apache 作
实际上,我们正在使用 Apache 网络服务器来托管我们的 REST-API。 脚本是用 Lua 编写的,并使用 mod-lua 映射。 例如来自 httpd.conf 的实际片段: [...] Lu
我在 apache 上的 ubuntu 中有一个虚拟主机,这不是我的主要配置,我有另一个网页作为我的主要网页,所以我想使用虚拟主机在同一个 IP 上设置这个。 urologyexpert.mx 是我的
我使用 Apache camel 已经很长时间了,发现它是满足各种系统集成相关业务需求的绝佳解决方案。但是几年前我遇到了 Apache Nifi 解决方案。经过一番谷歌搜索后,我发现虽然 Nifi 可
由于两者都是一次处理事件的流框架,这两种技术/流框架之间的核心架构差异是什么? 此外,在哪些特定用例中,一个比另一个更合适? 最佳答案 正如您所提到的,两者都是实时内存计算的流式平台。但是当您仔细观察
apache 文件(如 httpd.conf 和虚拟主机)中使用的语言名称是什么,例如 # Ensure that Apache listens on port 80 Listen 80 D
作为我学习过程的一部分,我认为如果我扩展更多关于 apache 的知识会很好。我有几个问题,虽然我知道有些内容可能需要相当冗长的解释,但我希望您能提供一个概述,以便我知道去哪里寻找。 (最好引用 mo
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 4 个月前关闭。 Improve
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
这个问题在这里已经有了答案: Difference Between Apache Kafka and Camel (Broker vs Integration) (4 个回答) 3年前关闭。 据我所知
我有 2 个使用相同规则的子域,如下所示: RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond
我是一名优秀的程序员,十分优秀!