Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android构造和语法错误_Java_Android - Fatal编程技术网

Java Android构造和语法错误

Java Android构造和语法错误,java,android,Java,Android,我在网上找到了这个示例代码。它似乎做了我想要的,向API发出请求,我只需要对它进行一点定制 然而,当我试图编译它时,它给了我三行相同的错误 Syntax error on token(s), misplaced construct(s) - Syntax error on token "setEntity", = expected after this token 也许有人能看到我看不到的东西 代码如下: import java.util.ArrayList; import java

我在网上找到了这个示例代码。它似乎做了我想要的,向API发出请求,我只需要对它进行一点定制

然而,当我试图编译它时,它给了我三行相同的错误

 Syntax error on token(s), misplaced 
 construct(s)
- Syntax error on token "setEntity", = 
 expected after this token
也许有人能看到我看不到的东西

代码如下:

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

public class http {

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");


    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("id", "12345"));
    nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    // Execute HTTP Post Request
    HttpResponse response = httpclient.execute(httppost);


 }
import java.util.ArrayList;
导入java.util.List;
导入org.apache.http.HttpResponse;
导入org.apache.http.NameValuePair;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.DefaultHttpClient;
公共类http{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://www.yoursite.com/script.php");
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“id”,“12345”);
添加(新的BasicNameValuePair(“stringdata”,“AndDev很酷!”);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
HttpResponse response=httpclient.execute(httppost);
}

除了Ran所说的之外,还会为nameValuePairs.add行和httppost.setEntity行抛出错误:您可能需要先学习一些基本的Java编程课程/教程。一些与编程相关的教程假定您已经熟悉了这一点,并且只列出了一些无法直接使用的代码行,因为它们属于方法内部

您需要按照如下方式重写类(“myMethodName”可以是您选择的任何其他名称)

公共类http{
public void myMethodName(){
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://www.yoursite.com/script.php");
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“id”,“12345”);
添加(新的BasicNameValuePair(“stringdata”,“AndDev很酷!”);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
HttpResponse response=httpclient.execute(httppost);
}
}

然后这段代码就不能按原样执行了。您需要创建一个类“http”的实例,并从Android活动中调用其“myMethodName”方法。

通过在错误的引号中添加“add”,更改nameValuePairs的错误代码应该在方法中。请阅读java教程。
public class http {
   public void myMethodName() {
       HttpClient httpclient = new DefaultHttpClient();
       HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");

       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
       nameValuePairs.add(new BasicNameValuePair("id", "12345"));
       nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

       // Execute HTTP Post Request
       HttpResponse response = httpclient.execute(httppost);
   }
}