2016-04-29 4 views
2

私はデータを取得するためにWebブラウザで表示することができますmySQLデータベース。しかし、私はアンドロイドスタジオv2.0で自分のデータを取得できませんでした。E/Volley:[1376] BasicNetwork.performRequest:http://192.168.0.109/connectphp/GetRestaurantData.php?restype=cafeの予期しない応答コード404

Configuration.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(); 
    } 
} 

Restaurant_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()); 
} 
?> 

何か?みんなに助けてください!

+0

404ページが見つかりません。パス 'http:// 192.168.0.109/connectphp/GetRestaurantData.php'は正しいですか? – aynber

+0

はいそれは正しい –

答えて

1

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){ 

     } 
    } 
} 
); 

また、あなたがJsonObjectRequstとJsonArrayRequestはvollyを使用して使用することができ、私が思うに、そのウルJsonArrayとしてPHPサービスからデータを渡すための文字列の要求よりもはるかに良いです。

+0

こんにちは私はあなたの方法で、最高の感謝の問題を解決しました、現在私は列の値の1つのタイプを入力するだけで、テーブル全体からデータを取得したい。どのように私はSQLクエリの書き込みを変更することができますか? $ sql = "レストランからのSELECT * restype = '"。$ restype。 "'"; $ r = mysqli_query($ conn、$ sql); $ res = mysqli_fetch_array($ r); $ result = array(); array_push($結果、アレイ( "restaurantid" => $ RES [ 'RESID']、 "restaurantname" => $ RES [ 'resnameは']、 "restaurantaddress" => $ RES [ 'resaddress'] 、 "レストランタイプ" => $ res ['restype']、 ) ); –

+0

@MickTehその解決されたウルの問題は私の答えを承認してください。 –