php - Android 和 PHP 连接到 MySQL 数据库

标签 php android mysql connection

我有以下代码,我正在努力让它工作。 Java 文件中有 2 个错误,我找不到修复方法。

这是我的 MySQLData.java 文件

package com.example.qosmetre2;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.net.ParseException;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 

import org.apache.http.NameValuePair; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.ListActivity; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.TextView;

public class MySQLData extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String result = null;
    InputStream is = null;
    StringBuilder sb=null;
    String result=null;
    //http post

    try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://localhost/android_test/fetch_data2.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    }catch(Exception e){
        Log.e("log_tag", "Error in http connection"+e.toString());
    }

    //convert response to string

    try{
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
        sb = new StringBuilder();
        sb.append(reader.readLine() + "\n");
        String line="0";
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }

        is.close();
        result=sb.toString();

    }catch(Exception e){

        Log.e("log_tag", "Error converting result "+e.toString());
    }
    //paring data

    String nam;
    int rec_pow;
    int tow;
    int stat;

    try{
    JSONArray jArray = new JSONArray(result);
    JSONObject json_data=null;

    for(int i=0;i<jArray.length();i++){
            json_data = jArray.getJSONObject(i);
            nam=json_data.getString("name");
            rec_pow=json_data.getInt("recieved_power");
            tow=json_data.getInt("tower");
            stat=json_data.getInt("status");

    }

    }catch(JSONException e1){
        Toast.makeText(getBaseContext(), "Could not Parse Data", Toast.LENGTH_LONG).show();
    }catch (ParseException e1){
        e1.printStackTrace();

    }

}

}

下面两行有错误:

String result=null;
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

第一个错误表示重复的局部变量结果,第二个错误表示无法将 nameValuePairs 解析为变量。

我还有一个放在我的 wamp 服务器中的 php 文件,我希望在我的本地服务器上显示的 infro 在应用程序中。我在某处读到我可能必须转发以便我可以通过互联网从应用程序访问,但我不知道。在修复错误之前,应用程序不会运行。

这是我的 PHP 文件 fetch_data2.php

  <?php
mysql_connect("888888","8888888","*******");
mysql_select_db("*******");
$q=mysql_query("SELECT * FROM users");


 while($row=mysql_fetch_assoc($q))
 $output[]=$row;

  print(json_encode($output));

  mysql_close();

?>

最佳答案

在查看您的代码时,我确实看到变量 result 定义了两次,但我没有看到任何地方定义了 nameValuePairs。我认为编译器是正确的:})

关于php - Android 和 PHP 连接到 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15823592/

相关文章:

javascript - 评级系统星级未根据其拥有的数量显示

php - CodeIgniter 在 form_dropdown 中选择值

android - 如何对 retrofit2 回调进行单元测试?

mysql - 无法在MySql工作台中导出数据

javascript - 使用 PHP 和 JavaScript 动态搜索 SQL 表并在 HTML 上显示

php - DateTime::modify 和 DST 开关

java - 二进制 XML 文件行 #0 : Error inflating class <unknown>

Android 应用程序错误报告

mysql - 测试前如何从开发数据库加载数据?

php - mysql 获取最近 10 秒内添加的记录?