异步任务不工作
Asynctask is not functioning
我正在尝试从 here 中提取数据。我想提取所有数据并将其打印在日志猫上,我正在使用 AsyncTask 来获取数据但是没有获取数据。
toast 没有显示,Log Cat window 也没有打印出任何东西。
这是我的代码:
package com.example.name.bill;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import org.json.JSONObject;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class Login extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
new MyTask();
}
private void readStream(InputStream in)
{
Log.e("TAG", in.toString());
}
class MyTask extends AsyncTask<URL , Integer, JSONObject>
{
@Override
protected void onPreExecute() {
}
@Override
protected JSONObject doInBackground(URL... s) {
try {
URL url = new URL("http://researchweb.iiit.ac.in/~name.jain/data");
Toast.makeText(Login.this, "Hello Name", Toast.LENGTH_SHORT).show();
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
urlConnection.getResponseCode();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
readStream(in);
urlConnection.disconnect();
} catch (java.io.IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
}
@Override
protected void onPostExecute(JSONObject jsonObject) {
}
}
}
请告诉我哪里错了。
注意:link中提供的数据不属于机密,仅供学习使用。
UPD1:
登录猫:
11-06 17:12:03.544 5433-5433/? I/art: Late-enabling -Xcheck:jni
11-06 17:12:04.008 5433-5477/com.example.name.bill D/OpenGLRenderer: Render dirty regions requested: true
11-06 17:12:04.026 5433-5433/com.example.name.bill D/Atlas: Validating map...
11-06 17:12:04.154 5433-5477/com.example.name.bill I/Adreno-EGL: <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.BF.1.1.04.04.02.162.107_msm8226_LA.BF.1.1__release_AU ()
11-06 17:12:04.154 5433-5477/com.example.name.bill I/Adreno-EGL: OpenGL ES Shader Compiler Version: E031.25.01.03
11-06 17:12:04.154 5433-5477/com.example.name.bill I/Adreno-EGL: Build Date: 10/28/14 Tue
11-06 17:12:04.154 5433-5477/com.example.name.bill I/Adreno-EGL: Local Branch:
11-06 17:12:04.154 5433-5477/com.example.name.bill I/Adreno-EGL: Remote Branch: quic/l_LNX.LA.3.6
11-06 17:12:04.154 5433-5477/com.example.name.bill I/Adreno-EGL: Local Patches: NONE
11-06 17:12:04.154 5433-5477/com.example.name.bill I/Adreno-EGL: Reconstruct Branch: AU_LINUX_ANDROID_LA.BF.1.1.04.04.02.162.107 + cb93e16 + f50fe49 + d7c18e6 + 5b9a565 + 0f3a25d + 607156e + 75511aa + e4d16c0 + 686f3eb + 211a271 + dd281ee + NOTHING
11-06 17:12:04.157 5433-5477/com.example.name.bill I/OpenGLRenderer: Initialized EGL, version 1.4
11-06 17:12:04.194 5433-5477/com.example.name.bill D/OpenGLRenderer: Enabling debug mode 0
11-06 17:12:08.274 5433-5684/com.example.name.bill E/TAG: java.io.BufferedInputStream@2ad62e1f
11-06 17:12:36.697 5433-5443/com.example.name.bill W/art: Suspending all threads took: 5.339ms
喜欢
new MyTask().execute();
并删除
Toast.makeText(Login.this, "Hello Lashit", Toast.LENGTH_SHORT).show();
来自 InBackground(....)
使用它..我已经用你的URL测试过...
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
new TheTask().execute("http://researchweb.iiit.ac.in/~lashit.jain/data");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
class TheTask extends AsyncTask<String,String,String>
{
ProgressDialog pd=null;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pd = new ProgressDialog(MainActivity.this);
pd.show();
}
@Override
protected String doInBackground(String... params) {
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost method = new HttpPost(params[0]);
HttpResponse response = httpclient.execute(method);
HttpEntity entity = response.getEntity();
if(entity != null){
return EntityUtils.toString(entity);
}
else{
return "No string.";
}
}
catch(Exception e){
return "Network problem";
}
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Log.e("result",result);
if(pd!=null) pd.dismiss();
}
}
}
我正在尝试从 here 中提取数据。我想提取所有数据并将其打印在日志猫上,我正在使用 AsyncTask 来获取数据但是没有获取数据。
toast 没有显示,Log Cat window 也没有打印出任何东西。
这是我的代码:
package com.example.name.bill;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import org.json.JSONObject;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class Login extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
new MyTask();
}
private void readStream(InputStream in)
{
Log.e("TAG", in.toString());
}
class MyTask extends AsyncTask<URL , Integer, JSONObject>
{
@Override
protected void onPreExecute() {
}
@Override
protected JSONObject doInBackground(URL... s) {
try {
URL url = new URL("http://researchweb.iiit.ac.in/~name.jain/data");
Toast.makeText(Login.this, "Hello Name", Toast.LENGTH_SHORT).show();
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
urlConnection.getResponseCode();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
readStream(in);
urlConnection.disconnect();
} catch (java.io.IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
}
@Override
protected void onPostExecute(JSONObject jsonObject) {
}
}
}
请告诉我哪里错了。
注意:link中提供的数据不属于机密,仅供学习使用。
UPD1:
登录猫:
11-06 17:12:03.544 5433-5433/? I/art: Late-enabling -Xcheck:jni
11-06 17:12:04.008 5433-5477/com.example.name.bill D/OpenGLRenderer: Render dirty regions requested: true
11-06 17:12:04.026 5433-5433/com.example.name.bill D/Atlas: Validating map...
11-06 17:12:04.154 5433-5477/com.example.name.bill I/Adreno-EGL: <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.BF.1.1.04.04.02.162.107_msm8226_LA.BF.1.1__release_AU ()
11-06 17:12:04.154 5433-5477/com.example.name.bill I/Adreno-EGL: OpenGL ES Shader Compiler Version: E031.25.01.03
11-06 17:12:04.154 5433-5477/com.example.name.bill I/Adreno-EGL: Build Date: 10/28/14 Tue
11-06 17:12:04.154 5433-5477/com.example.name.bill I/Adreno-EGL: Local Branch:
11-06 17:12:04.154 5433-5477/com.example.name.bill I/Adreno-EGL: Remote Branch: quic/l_LNX.LA.3.6
11-06 17:12:04.154 5433-5477/com.example.name.bill I/Adreno-EGL: Local Patches: NONE
11-06 17:12:04.154 5433-5477/com.example.name.bill I/Adreno-EGL: Reconstruct Branch: AU_LINUX_ANDROID_LA.BF.1.1.04.04.02.162.107 + cb93e16 + f50fe49 + d7c18e6 + 5b9a565 + 0f3a25d + 607156e + 75511aa + e4d16c0 + 686f3eb + 211a271 + dd281ee + NOTHING
11-06 17:12:04.157 5433-5477/com.example.name.bill I/OpenGLRenderer: Initialized EGL, version 1.4
11-06 17:12:04.194 5433-5477/com.example.name.bill D/OpenGLRenderer: Enabling debug mode 0
11-06 17:12:08.274 5433-5684/com.example.name.bill E/TAG: java.io.BufferedInputStream@2ad62e1f
11-06 17:12:36.697 5433-5443/com.example.name.bill W/art: Suspending all threads took: 5.339ms
喜欢
new MyTask().execute();
并删除
Toast.makeText(Login.this, "Hello Lashit", Toast.LENGTH_SHORT).show();
来自 InBackground(....)
使用它..我已经用你的URL测试过...
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
new TheTask().execute("http://researchweb.iiit.ac.in/~lashit.jain/data");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
class TheTask extends AsyncTask<String,String,String>
{
ProgressDialog pd=null;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pd = new ProgressDialog(MainActivity.this);
pd.show();
}
@Override
protected String doInBackground(String... params) {
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost method = new HttpPost(params[0]);
HttpResponse response = httpclient.execute(method);
HttpEntity entity = response.getEntity();
if(entity != null){
return EntityUtils.toString(entity);
}
else{
return "No string.";
}
}
catch(Exception e){
return "Network problem";
}
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Log.e("result",result);
if(pd!=null) pd.dismiss();
}
}
}