我有函数contador2(),它连接到webservices并返回URL中的数据。
问题是它没有返回任何内容,向我显示空变量repsonse2。
日志没有例外。
请帮助
package com.example.dona1click;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class Donar extends Fragment {
ImageButton insertar;
String emailAdd;
String name;
ImageView x;
ProgressDialog pd;
TextView text;
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View x = inflater.inflate(R.layout.donar, container, false);
// Capturo las variables que dejo en memoria, nombre y email
SharedPreferences sp1 = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
emailAdd = sp1.getString("EMAILADDRESS", "");
name = sp1.getString("NAME", "");
//-----------------------------------------------
insertar=(ImageButton) x.findViewById(R.id.imageButton1);
text=(TextView) x.findViewById(R.id.textView1);
text.setText(contador2());
insertar.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new Insertar(getActivity()).execute();
}
});
return x;
}
//Captura los datos del contador en el servidor.
private String contador2(){
// Declaro variables
String response2 = "";
HttpClient httpclient2 = new DefaultHttpClient();
HttpGet httppget;
HttpContext context = new BasicHttpContext();
HttpResponse res;
HttpEntity ent;
//-------------------------
httppget= new HttpGet("http://example.com/data.php"); // Url del Servidor a donde se envian las var post
try {
res = httpclient2.execute(httppget, context);
ent = res.getEntity();
response2 = EntityUtils.toString(ent);
//----------------------------------------------------------------
} catch (Exception e) {
// TODO Auto-generated catch block
}
Log.d("ADebugTag", "Value: " + response2);
return response2;
}
//Inserta los datos de las Personas en el servidor.
private String insertar(){
// Declaro variables
String response = "";
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
HttpPost httppost;
//-------------------------
httpclient=new DefaultHttpClient(); // abre la conexion con un cliente web
httppost= new HttpPost("http://example.co/insert.php"); // Url del Servidor a donde se envian las var post
//Añadimos nuestros datos en un arraylist
nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("email",emailAdd.toString().trim()));
nameValuePairs.add(new BasicNameValuePair("nombre",name.toString().trim()));
//---------------------------------------
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));// envio las variables
// capturo lo que devuelve, si no devolviera nada solo hago la ejecucion del httpclient sin el handler
ResponseHandler<String> responseHandler = new BasicResponseHandler();
response = httpclient.execute(httppost, responseHandler);
//----------------------------------------------------------------
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return response;
}
//AsyncTask para insertar Personas
class Insertar extends AsyncTask<String,String,String>{
private Activity context;
Insertar(Activity context){
this.context=context;
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String result = insertar();
return result;
}
@Override
protected void onPostExecute(String result) {
/*Toast.makeText(getActivity(), result,
Toast.LENGTH_SHORT).show(); */
if ( result.equals("0") ){
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setTitle("Dona1Click - Patrocinador");
alert.setMessage("Lo sentimos, ha excedido el limite diaro de 20 clicks");
alert.setNegativeButton("Close", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
alert.show();
}
if ( result.equals("1") ){
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setTitle("Dona1Click - Patrocinador");
WebView wv = new WebView(getActivity());
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
wv.getSettings().setAppCacheEnabled(false);
wv.loadUrl("https://www.dona1click.org/core/patro_app_mobile.php");
wv.setWebChromeClient(new WebChromeClient());
wv.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
if(pd.isShowing()&&pd!=null)
{
pd.dismiss();
}
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
});
alert.setView(wv);
alert.setNegativeButton("Close", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
alert.show();
// Para colocar un loading
pd = ProgressDialog.show(getActivity(), "", "Loading...",true);
//--------------------------
}
}
}
//AsyncTask para insertar Personas
class Contador extends AsyncTask<String,String,String>{
private Activity context;
Contador(Activity context){
this.context=context;
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
String result = "";
text.setText(result);
return result;
}
@Override
protected void onPostExecute(String result) {
/*Toast.makeText(getActivity(), result,
Toast.LENGTH_SHORT).show(); */
}
}
}