-1
私はlocalhostデータベースに接続し、同じからデータを取得する簡単なアンドロイドアプリを作ろうとしています。残念ながら、接続が中止されました
私はvolleyライブラリを使用していて、私のPHPスクリプトは正常に動作しています。私はGenymotionエミュレータを使用しています。アプリケーションを起動するたびに、「残念ながら接続が停止しました」というエラーメッセージが表示されます。
エミュレータに関連するものですか?
私の主な活動は次のとおりです。
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText editTextId;
private Button buttonGet;
private TextView textViewResult;
private ProgressDialog loading;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextId = (EditText)findViewById(R.id.editTextId);
buttonGet = (Button)findViewById(R.id.buttonGet);
textViewResult = (TextView)findViewById(R.id.textViewresult);
buttonGet.setOnClickListener(this);
}
private void getData(){
String id = editTextId.getText().toString().trim();
if(id.equals("")){
Toast.makeText(MainActivity.this, "Please enter an ID", Toast.LENGTH_SHORT).show();
return;
}
loading = ProgressDialog.show(this,"Please wait....","Fetching...",false,false);
String url = Config.DATA_URL+id;
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(MainActivity.this, error.getMessage(), Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void showJSON(String response){
String name="";
String address="";
String director="";
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
JSONObject collegeData = result.getJSONObject(0);
name = collegeData.getString(Config.KEY_NAME);
address = collegeData.getString(Config.KEY_ADDRESS);
director = collegeData.getString(Config.KEY_DIRECTOR);
} catch (JSONException e){
e.printStackTrace();
}
textViewResult.setText("Name:\t"+name+"Address:\t"+address+"Director:\t"+director);
}
@Override
public void onClick(View v) {
getData();
}
}
設定ファイル:
public class Config {
public static final String DATA_URL = "http://127.0.0.1/getDatanew.php?id=";
public static final String KEY_NAME = "name";
public static final String KEY_ADDRESS = "address";
public static final String KEY_DIRECTOR = "director";
public static final String JSON_ARRAY = "result";
}
dataConnect.phpファイル:
<?php
$con = new mysqli("localhost","root","","college") or die ('Unable to Connect');
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
?>
getData.phpが
<?php
//if($_SERVER['REQUESTED_METHOD']=='GET'){
if($_GET['id']){
$id = $_GET['id'];
if($id)
//echo 'hello '.$id;
require_once('dbConnectnew.php');
//echo $con;
$sql = "SELECT * FROM college WHERE id='".$id."'";
$r = mysqli_query($con,$sql);
//if(!$r){
//printf("Error: %s\n", mysqli_error($con));
//出口()ファイル。 //}
$res = mysqli_fetch_array($r);
$result = array();
array_push($result,array(
"name"=>$res['name'],
"address"=>$res['address'],
"director"=>$res['director']
)
);
echo json_encode(array("result"=>$result));
mysqli_close($con);
}else echo 'no if';
?>
あなたのコードを投稿してください...! –
もっと簡潔にする –
コードが含まれています –