涉及url传递的asyntask中的致命错误

时间:2014-02-11 10:38:43

标签: java android

我是android新手,我正在尝试解决一个runtimeException错误,为什么会发生这种情况?扫管笏是错的? 这是doInBackgroundLogin方法

 public void doInBackgroundLogin()
{   
                String link = "http://www.quinoid.com/project/AndroidXml/Login.php?username="
                               + txtUserName.getText().toString() + "&password=" + txtPassword.getText().toString();
                try {                       
                        JSONObject json = jParser.getJSONFromUrl(link);                             
                } 
                catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Intent myIntent = new Intent(MainActivity.this, secondpage.class);
                startActivity(myIntent);
}

这是json解析器方法

 public JSONObject getJSONFromUrl(String url) throws JSONException {
 try 
 {
 DefaultHttpClient httpClient = new DefaultHttpClient();
 HttpPost httpPost = new HttpPost(url);//error occurs here
 HttpResponse httpResponse = httpClient.execute(httpPost);
 HttpEntity httpEntity = httpResponse.getEntity();
 is = httpEntity.getContent();          
 }

这是堆栈跟踪

02-11 05:20:40.077: E/AndroidRuntime(1754): FATAL EXCEPTION: AsyncTask #2
02-11 05:20:40.077: E/AndroidRuntime(1754): Process: com.example.qwedd, PID: 1754
02-11 05:20:40.077: E/AndroidRuntime(1754): java.lang.RuntimeException: An error occured while executing doInBackground()
02-11 05:20:40.077: E/AndroidRuntime(1754):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
02-11 05:20:40.077: E/AndroidRuntime(1754):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
02-11 05:20:40.077: E/AndroidRuntime(1754):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
02-11 05:20:40.077: E/AndroidRuntime(1754):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
02-11 05:20:40.077: E/AndroidRuntime(1754):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
02-11 05:20:40.077: E/AndroidRuntime(1754):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
02-11 05:20:40.077: E/AndroidRuntime(1754):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
02-11 05:20:40.077: E/AndroidRuntime(1754):     at java.lang.Thread.run(Thread.java:841)
02-11 05:20:40.077: E/AndroidRuntime(1754): Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
02-11 05:20:40.077: E/AndroidRuntime(1754):     at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
02-11 05:20:40.077: E/AndroidRuntime(1754):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
02-11 05:20:40.077: E/AndroidRuntime(1754):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
02-11 05:20:40.077: E/AndroidRuntime(1754):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
02-11 05:20:40.077: E/AndroidRuntime(1754):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
02-11 05:20:40.077: E/AndroidRuntime(1754):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
02-11 05:20:40.077: E/AndroidRuntime(1754):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
02-11 05:20:40.077: E/AndroidRuntime(1754):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
02-11 05:20:40.077: E/AndroidRuntime(1754):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
02-11 05:20:40.077: E/AndroidRuntime(1754):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
02-11 05:20:40.077: E/AndroidRuntime(1754):     at com.example.connection.JSONParser.getJSONFromUrl(JSONParser.java:38)
02-11 05:20:40.077: E/AndroidRuntime(1754):     at com.example.qwedd.MainActivity.doInBackgroundLogin(MainActivity.java:57)
02-11 05:20:40.077: E/AndroidRuntime(1754):     at com.example.qwedd.MainActivity$Connection.doInBackground(MainActivity.java:75)

2 个答案:

答案 0 :(得分:1)

试试这个..

java.lang.SecurityException: Permission denied (missing INTERNET permission?)

在清单中添加权限

<uses-permission android:name="android.permission.INTERNET" />

</application>之后或之前<application>并查看此Doc

答案 1 :(得分:0)

只需将此权限添加到 manifest.xml 文件中:

<uses-permission android:name="android.permission.INTERNET" />