我在我正在写作的asynctask例程中用我的varibles稍微唠叨
我的代码:
package com.example.deceunincktechniekers;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import java.io.*;
import java.util.LinkedList;
import java.util.List;
import org.apache.http.client.*;
import org.apache.http.*;
import org.json.JSONException;
import org.json.JSONObject;
public class BTWcontrole extends Activity
{
TextView btwuitvoer;
EditText landcode;
EditText nummer;
Button verstuur;
String land;
String num;
String antwoord;
Context btwcontrolecontext;
String jsonData;
private String bedrijfsnaam = new String();
String zoekurl = "http://www.softnaert.be/SNWebservices/SNWeb.wso/checkVat/JSON";
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.btwcontrole);
btwcontrolecontext = this;
btwuitvoer = (TextView) findViewById(R.id.resultaat);
verstuur = (Button) findViewById(R.id.controleer);
landcode = (EditText) findViewById(R.id.landcode);
nummer = (EditText) findViewById(R.id.btwnr);
verstuur.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
btwopvragen loaderTask = new btwopvragen();
loaderTask.execute();
land = landcode.getText().toString();
num = nummer.getText().toString();
Log.i("bedrijfsnaam in onclick", bedrijfsnaam);
btwuitvoer.setText(bedrijfsnaam);
}
});};
public void onTaskComplete(){
Log.i("bedrijfsnaam ontaskcomplete", bedrijfsnaam);
}
private class btwopvragen extends AsyncTask<Void, Void, Void>{
ProgressDialog dialog;
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(btwcontrolecontext);
dialog.setTitle("btwnummer ophalen");
dialog.show();
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
// toegevoegde code om parameters aan de code toe te voegen
if(!zoekurl.endsWith("?")){
zoekurl += "?";
};
List<NameValuePair> parameters = new LinkedList<NameValuePair>();
parameters.add(new BasicNameValuePair("llcountryCode", String.valueOf(land)));
parameters.add(new BasicNameValuePair("llvatNumber", String.valueOf(num)));
String parameterString = URLEncodedUtils.format(parameters, "utf-8");
// effectieve http request met de parameters toegevoegd
HttpClient client = new DefaultHttpClient();
HttpGet aanvraag = new HttpGet(zoekurl + parameterString);
// foutanalyse van de http request
try
{
HttpResponse antwoord = client.execute(aanvraag);
StatusLine statuslijn = antwoord.getStatusLine();
int statuscode = statuslijn.getStatusCode();
if(statuscode != 200){
return null;
}
InputStream jsonStream = antwoord.getEntity().getContent();
BufferedReader reader= new BufferedReader(new InputStreamReader(jsonStream));
StringBuilder builder = new StringBuilder();
String lijn;
while((lijn = reader.readLine())!= null){
builder.append(lijn);
}
jsonData = builder.toString();
// met de volgende lijn is de data zichtbaar in de logcat
Log.i("btw geretourneerde data", jsonData);
// hier beginnen we met de json data te ontmantelen
JSONObject json = new JSONObject(jsonData);
String bedrijfsnaam = json.getString("sCompanyName");
Log.i("bedrijfsnaam in asynctask", bedrijfsnaam);
}
catch (ClientProtocolException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
Log.i("bedrijfsnaam in onpostexecute", bedrijfsnaam);
dialog.dismiss();
super.onPostExecute(result);
}
}
}
我的logcat:
08-11 14:22:45.160: I/Choreographer(6532): Skipped 42 frames! The application may be doing too much work on its main thread.
08-11 14:22:45.439: I/Choreographer(6532): Skipped 30 frames! The application may be doing too much work on its main thread.
08-11 14:22:45.649: I/Choreographer(6532): Skipped 39 frames! The application may be doing too much work on its main thread.
08-11 14:22:45.849: I/Choreographer(6532): Skipped 50 frames! The application may be doing too much work on its main thread.
08-11 14:22:46.019: I/Choreographer(6532): Skipped 44 frames! The application may be doing too much work on its main thread.
08-11 14:22:46.209: I/Choreographer(6532): Skipped 47 frames! The application may be doing too much work on its main thread.
08-11 14:22:46.329: I/Choreographer(6532): Skipped 30 frames! The application may be doing too much work on its main thread.
08-11 14:22:46.479: I/Choreographer(6532): Skipped 38 frames! The application may be doing too much work on its main thread.
08-11 14:22:46.629: I/Choreographer(6532): Skipped 39 frames! The application may be doing too much work on its main thread.
08-11 14:22:46.699: I/btw geretourneerde data(6532): { "eTransferStatus": 0, "bValid": true, "sLand": "be", "sBtwNr": "0420347421", "sCompanyName": "BVBA SOFT - NAERT", "sCompanyAdres": "MARIASTRAAT 4\n8870 IZEGEM", "sTransferError": "", "sFaultCode": "", "sFaultString": "", "sErr": "", "dDateChecked": "2013-08-11"}
08-11 14:22:46.699: I/bedrijfsnaam in asynctask(6532): BVBA SOFT - NAERT
08-11 14:22:46.779: I/Choreographer(6532): Skipped 36 frames! The application may be doing too much work on its main thread.
08-11 14:22:46.959: I/Choreographer(6532): Skipped 43 frames! The application may be doing too much work on its main thread.
08-11 14:23:10.299: I/Choreographer(6532): Skipped 30 frames! The application may be doing too much work on its main thread.
问题是我无法在我的oncreate方法中使用我的变量bedrijfsnaam,我在不同的地方推出了4行,以使我的logcat中的bedrijfsnaam变量可见,并且只有一个似乎在记录。
可能是什么原因?
答案 0 :(得分:0)
*我没有测试过这段代码。但我在Asyntask做了一些改变。你已经全局初始化了String。我不知道为什么你再次宣布。在这个代码中我修改了doinBackground方法完成后。并执行onPostExecution方法UI将更新。
public class BTWcontrole extends Activity {
TextView btwuitvoer;
EditText landcode;
EditText nummer;
Button verstuur;
String land;
String num;
String antwoord;
Context btwcontrolecontext;
String jsonData;
private String bedrijfsnaam = new String();
String zoekurl = "http://www.softnaert.be/SNWebservices/SNWeb.wso/checkVat/JSON";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.btwcontrole);
btwcontrolecontext = this;
btwuitvoer = (TextView) findViewById(R.id.resultaat);
verstuur = (Button) findViewById(R.id.controleer);
landcode = (EditText) findViewById(R.id.landcode);
nummer = (EditText) findViewById(R.id.btwnr);
verstuur.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
btwopvragen loaderTask = new btwopvragen();
loaderTask.execute();
}
});
};
public void onTaskComplete() {
Log.i("bedrijfsnaam ontaskcomplete", bedrijfsnaam);
}
private class btwopvragen extends AsyncTask<Void, Void, Void> {
ProgressDialog dialog;
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(btwcontrolecontext);
dialog.setTitle("btwnummer ophalen");
dialog.show();
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
// toegevoegde code om parameters aan de code toe te voegen
if (!zoekurl.endsWith("?")) {
zoekurl += "?";
}
;
List<NameValuePair> parameters = new LinkedList<NameValuePair>();
parameters.add(new BasicNameValuePair("llcountryCode", String
.valueOf(land)));
parameters.add(new BasicNameValuePair("llvatNumber", String
.valueOf(num)));
String parameterString = URLEncodedUtils
.format(parameters, "utf-8");
// effectieve http request met de parameters toegevoegd
HttpClient client = new DefaultHttpClient();
HttpGet aanvraag = new HttpGet(zoekurl + parameterString);
// foutanalyse van de http request
try {
HttpResponse antwoord = client.execute(aanvraag);
StatusLine statuslijn = antwoord.getStatusLine();
int statuscode = statuslijn.getStatusCode();
if (statuscode != 200) {
return null;
}
InputStream jsonStream = antwoord.getEntity().getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(jsonStream));
StringBuilder builder = new StringBuilder();
String lijn;
while ((lijn = reader.readLine()) != null) {
builder.append(lijn);
}
jsonData = builder.toString();
// met de volgende lijn is de data zichtbaar in de logcat
Log.i("btw geretourneerde data", jsonData);
// hier beginnen we met de json data te ontmantelen
JSONObject json = new JSONObject(jsonData);
bedrijfsnaam = json.getString("sCompanyName");
Log.i("bedrijfsnaam in asynctask", bedrijfsnaam);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
Log.i("bedrijfsnaam in onpostexecute", bedrijfsnaam);
dialog.dismiss();
land = landcode.getText().toString();
num = nummer.getText().toString();
Log.i("bedrijfsnaam in onclick", bedrijfsnaam);
btwuitvoer.setText(bedrijfsnaam);
super.onPostExecute(result);
}
}}
答案 1 :(得分:0)