- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我设法在 Web 浏览器上显示以从 mySQL 数据库中检索我的数据。但是我无法在我的 android studio v2.0 中检索我的数据。
配置.java类
public class configuration {
public static final String DATA_URL = "http://192.168.0.109/connectphp/GetRestaurantData.php?restype=";
public static final String KEY_ID = "restaurantid";
public static final String KEY_name = "restaurantname";
public static final String KEY_address = "restaurantaddress";
public static final String KEY_type = "restauranttype";
public static final String KEY_lat = "restaurantlat";
public static final String KEY_long = "restaurantlong";
public static final String KEY_hp = "restauranthp";
public static final String JSON_ARRAY = "result";
}
餐厅页面类
package com.example.teh.fyp_tp028725;
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
@SuppressLint("NewApi")
public class RestaurantPage extends ActionBarActivity implements View.OnClickListener{
private ListView searchresultview;
private ProgressDialog loading;
EditText restauranttext;
Button press;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_restaurant_page);
searchresultview = (ListView) findViewById(R.id.restaurantlist);
restauranttext = (EditText)findViewById(R.id.restauranttypetext);
press = (Button)findViewById(R.id.searchbutton);
press.setOnClickListener(this);
}
public void getData(){
String type = restauranttext.getText().toString().trim();
if (type.equals("")){
Toast.makeText(this, "Please enter an id", Toast.LENGTH_LONG).show();
return;
}
loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false);
String url = configuration.DATA_URL+restauranttext.getText().toString().trim();
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
loading.dismiss();
showJSON(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(RestaurantPage.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void showJSON(String response) {
String name = "";
String address = "";
String type = "";
String latitude = "";
String longitude = "";
String hp = "";
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(configuration.JSON_ARRAY);
JSONObject bookData = result.getJSONObject(0);
name = bookData.getString(configuration.KEY_name);
address = bookData.getString(configuration.KEY_address);
type = bookData.getString(configuration.KEY_type);
hp = bookData.getString(configuration.KEY_hp);
//latitude = bookData.getString(configuration.KEY_lat);
//longitude = bookData.getString(configuration.KEY_long);
ArrayList<HashMap<String, String>> bookList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("restaurantname", "Place Name : " + name);
map.put("restaurantaddress", "Address : " + address);
map.put("restauranttype", "Place Type : " + type);
map.put("restauranthp", "Phone Number : " + hp);
bookList.add(map);
SimpleAdapter simpleAdapter = new SimpleAdapter(this, bookList, R.layout.customizelayout, new String[]{"restaurantname", "restaurantaddress", "restauranttype", "restauranthp"}, new int[]{R.id.textViewname, R.id.textViewaddress, R.id.textViewtype, R.id.textViewhp});
searchresultview.setAdapter(simpleAdapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onClick(View v) {
getData();
}
}
餐厅_page_xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.teh.fyp_tp028725.RestaurantPage">
<TextView
android:id="@+id/logobar"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="Restaurant"
android:background="#87F6FF"
android:textColor="#000000"
android:textSize="30sp"
android:textStyle="bold"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/restauranttypetext"
android:hint="Enter type of restaurant here"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:maxLines="2"
android:layout_below="@+id/logobar"
android:layout_marginTop="30dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search"
android:id="@+id/searchbutton"
android:layout_below="@+id/restauranttypetext"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp" />
<ImageView
android:layout_width="150dp"
android:layout_height="50dp"
android:id="@+id/poweredBy"
android:src="@drawable/powered_by_google_light"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/restaurantlist"
android:layout_below="@+id/searchbutton"
android:layout_marginTop="10dp"
android:layout_above="@+id/poweredBy"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
GetRestaurantData.php 类
<?php
if($_SERVER['REQUEST_METHOD']=='GET'){
$restype = $_GET['restype'];
require_once('Connection.php');
$sql = "SELECT * FROM restaurant WHERE restype='".$restype."'";
$r = mysqli_query($conn,$sql);
$res = mysqli_fetch_array($r);
$result = array();
array_push($result,array(
"restaurantid"=>$res['resID'],
"restaurantname"=>$res['resname'],
"restaurantaddress"=>$res['resaddress'],
"restauranttype"=>$res['restype'],
"restaurantlat"=>$res['reslat'],
"restaurantlong"=>$res['reslong'],
"restauranthp"=>$res['reshpnumber'],
)
);
echo json_encode(array("result"=>$result));
mysqli_close($conn);
}
Connection.php 类
<?php
$servername = "localhost"; //replace it with your database server name
$username = "root"; //replace it with your database username
$password = ""; //replace it with your database password
$dbname = "fyptp028725";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
我在这里做错了什么吗?请帮助大家!
最佳答案
如果您收到 404 响应,它应该进入您设置的任何错误监听器。您在错误监听器中获得了一个 VolleyError 对象。您可以从此对象获取网络响应,然后从响应主体获取数据。它以 char 数组的形式提供,因此您需要自己将其转换为其他内容。
StringRequest request = new StringRequest( Request.Method.GET, "yourURL", new Response.Listener<String>() {
@Override
public void onResponse( String s ) {
//Do whatever
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse( VolleyError volleyError ) {
try {
String responseBody = new String( volleyError.networkResponse.data, "utf-8" );
JSONObject jsonObject = new JSONObject( responseBody );
} catch ( JSONException e ) {
//Handle a malformed json response
} catch (UnsupportedEncodingException error){
}
}
}
);
您还可以通过 volly 使用 JsonObjectRequst 和 JsonArrayRequest,我认为它比字符串请求好得多,因为您将来自 php 服务的数据作为 JsonArray 传递。
关于php - E/ Volley : [1376] BasicNetwork. 执行请求 : Unexpected response code 404 for http://192. 168.0.109/connectphp/GetRestaurantData.php?restype=cafe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36943889/
一旦在 qconsole Marklogic 中运行以下代码,我就会遇到以下错误 XDMP-UNEXPECTED: (err:XPST0003) Unexpected token syntax err
我已经在我的包中编写了这个函数。 def partitionIntoDays(ls, number, lookupKey=None): ''' Partitions the location
我只是一个 android 初学者,我已经安装了 Android Studio(版本是 1.0.2),并创建了一个新的空白应用程序,按照名为“构建你的第一个应用程序”的官方教程,我学习到这个页面' h
这只是前几天工作,但我刚刚将我的代码更新到运行乘客 2.2.4 的审查服务器,而我的 2.3.4 rails 应用程序现在无法在那个盒子上启动。 乘客报告: Passenger encountered
我正在尝试使用带有 Angular 2的整页, 将其导入我的 app.module.ts 时出现以下错误。 "(SystemJS) Unexpected token ) at Obje
TFS2015 vNext 构建失败并出现记录器错误(下面附有错误消息)。根据我的调查,这看起来与 CentralLogger - "Microsoft.TeamFoundation.Distribu
计算机科学学校项目。我需要编写一个程序,其中用户声明数组的大小,然后以数字、非递减顺序填充数组,然后声明一个值 x。然后将 X 分配到适当的位置,以便整个数组按数字、非递减顺序排列。然后输出该数组。
在这 2 个方法中,inspect1 显示编译错误“Unexpected bound”而 inspect2 工作正常,为什么? public void inspect1(List u){ S
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我正在尝试运行以下代码,但遇到了“此时意外”错误。 (echo COPY (SELECT ta.colA as name, ta.colB as user_e, ta.colC as user_n,
我有以下查询: select u.UserName, count(*) as total from Voting v join User u using (UserID) where unique (
我们有以下查询在 MSSQL 中完美运行但在 MySQL 中无法运行: select CONVERT(datetime, dateVal) as DateOccurred, itemID, COUNT
我的代码中存在缩进错误问题。它看起来是正确的...有人能指出我做错了什么吗?我的查询行不断收到错误。 def invoice_details(myDeliveryID): conn = pym
我有以下代码: int a , b , sum; cin>>a>>b; sum=a+b; cout>a>>b>>c; cout<
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我在一个批处理文件上运行这个命令: for %I in (*.txt *.doc) do copy %I c:\test2 ...它不断返回: I was unexpected at this tim
创建查询时出现错误: 'from' unexpected 我的代码如下: @Override public Admin findByAdmin(Admin admin) {
我正在尝试运行此 python 代码,但我不断收到错误消息“意外缩进”。我不确定怎么了。间距似乎很好。有什么想法吗? services = ['Service1'] for service in
我在名为“circular_dependency”的目录中有一些 python 文件: 导入文件_1.py: from circular_dependency.import_file_2 import
我正在尝试使用 gcc 编译代码并运行可执行文件,但它抛出错误: gcc somefile.c -o somefile 编译成功。但是,当我尝试执行它时: $sh somefile 它导致:语法错误:
我是一名优秀的程序员,十分优秀!