【问题标题】:Errors in client side (Android) during connecting to server side (PHP) in Netbeans在 Netbeans 中连接到服务器端 (PHP) 期间客户端 (Android) 出现错误
【发布时间】:2016-01-18 06:17:36
【问题描述】:

我正在尝试使用 netbeans 将客户端(Android 应用程序)连接到服务器(PHP)。为此,我在 htdocs 文件夹中编写了一个名为“getAllPeopleBornAfter.php”的 PHP 代码,该代码运行正常。

<?php
mysql_connect("localhost","root","");
mysql_select_db("test");

$q=mysql_query("SELECT * FROM android WHERE ID>'".$_REQUEST['id']."'");
while($e=mysql_fetch_assoc($q))
        $output[]=$e;

print(json_encode($output));

mysql_close();
?>

我已经通过 netbeans 创建了一个新的 javaApplications 并在其中编写了以下代码。

package javaapplication4;

public class JavaApplication4 {


    public static void main(String[] args) {
        String result = "";
//the year data to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("id","1"));

try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("localhost/getAllPeopleBornAfter.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        InputStream 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);
        StringBuilder sb = new StringBuilder();
        String line = null;
        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());
}

//parse json data
try{
        JSONArray jArray = new JSONArray(result);
        for(int i=0;i<jArray.length();i++){
                JSONObject json_data = jArray.getJSONObject(i);
                Log.i("log_tag","id: "+json_data.getInt("ID")+
                        ", description: "+json_data.getString("Description")
                );
        }
}
catch(JSONException e){
        Log.e("log_tag", "Error parsing data "+e.toString());
}
    }

}

但是,它给了我一个错误

    ant -f C:\\xampp\\htdocs\\JavaApplication4 -Dnb.internal.action.name=run run
init:
deps-jar:
Created dir: C:\xampp\htdocs\JavaApplication4\build
Updating property file: C:\xampp\htdocs\JavaApplication4\build\built-jar.properties
Created dir: C:\xampp\htdocs\JavaApplication4\build\classes
Created dir: C:\xampp\htdocs\JavaApplication4\build\empty
Created dir: C:\xampp\htdocs\JavaApplication4\build\generated-sources\ap-source-output
Compiling 1 source file to C:\xampp\htdocs\JavaApplication4\build\classes
C:\xampp\htdocs\JavaApplication4\src\javaapplication4\JavaApplication4.java:10: error: cannot find symbol
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
  symbol:   class ArrayList
  location: class JavaApplication4
C:\xampp\htdocs\JavaApplication4\src\javaapplication4\JavaApplication4.java:10: error: cannot find symbol
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
  symbol:   class NameValuePair
  location: class JavaApplication4
C:\xampp\htdocs\JavaApplication4\src\javaapplication4\JavaApplication4.java:10: error: cannot find symbol
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
  symbol:   class ArrayList
  location: class JavaApplication4
C:\xampp\htdocs\JavaApplication4\src\javaapplication4\JavaApplication4.java:10: error: cannot find symbol
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
  symbol:   class NameValuePair
  location: class JavaApplication4
C:\xampp\htdocs\JavaApplication4\src\javaapplication4\JavaApplication4.java:11: error: cannot find symbol
nameValuePairs.add(new BasicNameValuePair("id","1"));
  symbol:   class BasicNameValuePair
  location: class JavaApplication4
C:\xampp\htdocs\JavaApplication4\src\javaapplication4\JavaApplication4.java:14: error: cannot find symbol
        HttpClient httpclient = new DefaultHttpClient();
  symbol:   class HttpClient
  location: class JavaApplication4
C:\xampp\htdocs\JavaApplication4\src\javaapplication4\JavaApplication4.java:14: error: cannot find symbol
        HttpClient httpclient = new DefaultHttpClient();
  symbol:   class DefaultHttpClient
  location: class JavaApplication4
C:\xampp\htdocs\JavaApplication4\src\javaapplication4\JavaApplication4.java:15: error: cannot find symbol
        HttpPost httppost = new HttpPost("localhost/getAllPeopleBornAfter.php");
  symbol:   class HttpPost
  location: class JavaApplication4
C:\xampp\htdocs\JavaApplication4\src\javaapplication4\JavaApplication4.java:15: error: cannot find symbol
        HttpPost httppost = new HttpPost("localhost/getAllPeopleBornAfter.php");
  symbol:   class HttpPost
  location: class JavaApplication4
C:\xampp\htdocs\JavaApplication4\src\javaapplication4\JavaApplication4.java:16: error: cannot find symbol
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
  symbol:   class UrlEncodedFormEntity
  location: class JavaApplication4
C:\xampp\htdocs\JavaApplication4\src\javaapplication4\JavaApplication4.java:17: error: cannot find symbol
        HttpResponse response = httpclient.execute(httppost);
  symbol:   class HttpResponse
  location: class JavaApplication4
C:\xampp\htdocs\JavaApplication4\src\javaapplication4\JavaApplication4.java:18: error: cannot find symbol
        HttpEntity entity = response.getEntity();
  symbol:   class HttpEntity
  location: class JavaApplication4
C:\xampp\htdocs\JavaApplication4\src\javaapplication4\JavaApplication4.java:19: error: cannot find symbol
        InputStream is = entity.getContent();
  symbol:   class InputStream
  location: class JavaApplication4
C:\xampp\htdocs\JavaApplication4\src\javaapplication4\JavaApplication4.java:21: error: cannot find symbol
        Log.e("log_tag", "Error in http connection "+e.toString());
  symbol:   variable Log
  location: class JavaApplication4
C:\xampp\htdocs\JavaApplication4\src\javaapplication4\JavaApplication4.java:25: error: cannot find symbol
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
  symbol:   class BufferedReader
  location: class JavaApplication4
C:\xampp\htdocs\JavaApplication4\src\javaapplication4\JavaApplication4.java:25: error: cannot find symbol
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
  symbol:   class BufferedReader
  location: class JavaApplication4
C:\xampp\htdocs\JavaApplication4\src\javaapplication4\JavaApplication4.java:25: error: cannot find symbol
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
  symbol:   class InputStreamReader
  location: class JavaApplication4
C:\xampp\htdocs\JavaApplication4\src\javaapplication4\JavaApplication4.java:25: error: cannot find symbol
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
  symbol:   variable is
  location: class JavaApplication4
C:\xampp\htdocs\JavaApplication4\src\javaapplication4\JavaApplication4.java:31: error: cannot find symbol
        is.close();
  symbol:   variable is
  location: class JavaApplication4
C:\xampp\htdocs\JavaApplication4\src\javaapplication4\JavaApplication4.java:35: error: cannot find symbol
        Log.e("log_tag", "Error converting result "+e.toString());
  symbol:   variable Log
  location: class JavaApplication4
C:\xampp\htdocs\JavaApplication4\src\javaapplication4\JavaApplication4.java:40: error: cannot find symbol
        JSONArray jArray = new JSONArray(result);
  symbol:   class JSONArray
  location: class JavaApplication4
C:\xampp\htdocs\JavaApplication4\src\javaapplication4\JavaApplication4.java:40: error: cannot find symbol
        JSONArray jArray = new JSONArray(result);
  symbol:   class JSONArray
  location: class JavaApplication4
C:\xampp\htdocs\JavaApplication4\src\javaapplication4\JavaApplication4.java:42: error: cannot find symbol
                JSONObject json_data = jArray.getJSONObject(i);
  symbol:   class JSONObject
  location: class JavaApplication4
C:\xampp\htdocs\JavaApplication4\src\javaapplication4\JavaApplication4.java:43: error: cannot find symbol
                Log.i("log_tag","id: "+json_data.getInt("ID")+
  symbol:   variable Log
  location: class JavaApplication4
C:\xampp\htdocs\JavaApplication4\src\javaapplication4\JavaApplication4.java:48: error: cannot find symbol
catch(JSONException e){
  symbol:   class JSONException
  location: class JavaApplication4
C:\xampp\htdocs\JavaApplication4\src\javaapplication4\JavaApplication4.java:49: error: cannot find symbol
        Log.e("log_tag", "Error parsing data "+e.toString());
  symbol:   variable Log
  location: class JavaApplication4
26 errors
C:\xampp\htdocs\JavaApplication4\nbproject\build-impl.xml:923: The following error occurred while executing this line:
C:\xampp\htdocs\JavaApplication4\nbproject\build-impl.xml:263: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 0 seconds)

由于我是java初学者,请任何人指导我。 谢谢。

【问题讨论】:

    标签: java android json netbeans mobile-application


    【解决方案1】:

    更新

    请查看this答案。

    原答案:

    请查看您如何实例化变量等。

    在你的代码ArrayList&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;();

    你从哪里得到&lt;NameValuePair&gt;?它应该像这样实例化:

    ArrayList&lt;String&gt; TheNameOfYourList = new ArrayList&lt;String&gt;();

    阅读并查看您的日志。这很简单。

    【讨论】:

    • 当我测试 ArrayList nameValuePairs = new ArrayList(); 时它给了我同样的错误
    猜你喜欢
    • 2015-03-21
    • 1970-01-01
    • 2014-07-04
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多