Android 使用HTTP GET请求检索数据时出错 公共类BackgroundWorker扩展异步任务{ 公共静态最终int连接\u超时=7000; 公共静态最终整型读取超时=15000; 公共字符串类型、用户名、密码、url; 公共HttpURLConnection; 公共StringBuilder StringBuilder; 公共输入流输入; 公共缓冲读取器; 公共URL myURL; 公共加载; 公共警报对话框; 公共语境; BackgroundWorker(上下文ctxt){ 上下文=ctxt; con=null; } @凌驾 受保护的字符串doInBackground(字符串…参数){ this.type=params[0]; this.url=params[1]; this.username=params[2]; this.password=params[3]; 如果(this.type==“POST”) { //我在这里处理邮件请求 } if(this.type==“GET”) { 试一试{ this.myURL=新URL(this.URL); this.con=(HttpURLConnection)this.myURL.openConnection(); this.con.setRequestMethod(this.type); 此.con.setReadTimeout(读取超时); 此.con.setConnectTimeout(连接超时); this.con.setRequestProperty(“内容类型”、“应用程序/json”); 此.con.setRequestProperty(“接受”、“应用程序/json”); this.con.connect(); this.input=this.con.getInputStream(); this.reader=新的BufferedReader(新的InputStreamReader(输入,“UTF-8”),8; stringBuilder=新的stringBuilder(); 字符串输入线; 而((inputLine=reader.readLine())!=null){ stringBuilder.append(inputLine); } reader.close(); 返回此.con.toString(); } 捕获(IOE异常){ e、 printStackTrace(); //iam执行get请求事件时在catch块中找到编译器 返回String.format(“url%s\n%s\n%s”,e.getMessage(),this.type,this.url); }最后{ this.con.disconnect(); } } 返回null; }
我看不到任何Get参数附加到您的web服务中。这一定是原因。您需要随请求发送参数。下面是从代码中提取的代码Android 使用HTTP GET请求检索数据时出错 公共类BackgroundWorker扩展异步任务{ 公共静态最终int连接\u超时=7000; 公共静态最终整型读取超时=15000; 公共字符串类型、用户名、密码、url; 公共HttpURLConnection; 公共StringBuilder StringBuilder; 公共输入流输入; 公共缓冲读取器; 公共URL myURL; 公共加载; 公共警报对话框; 公共语境; BackgroundWorker(上下文ctxt){ 上下文=ctxt; con=null; } @凌驾 受保护的字符串doInBackground(字符串…参数){ this.type=params[0]; this.url=params[1]; this.username=params[2]; this.password=params[3]; 如果(this.type==“POST”) { //我在这里处理邮件请求 } if(this.type==“GET”) { 试一试{ this.myURL=新URL(this.URL); this.con=(HttpURLConnection)this.myURL.openConnection(); this.con.setRequestMethod(this.type); 此.con.setReadTimeout(读取超时); 此.con.setConnectTimeout(连接超时); this.con.setRequestProperty(“内容类型”、“应用程序/json”); 此.con.setRequestProperty(“接受”、“应用程序/json”); this.con.connect(); this.input=this.con.getInputStream(); this.reader=新的BufferedReader(新的InputStreamReader(输入,“UTF-8”),8; stringBuilder=新的stringBuilder(); 字符串输入线; 而((inputLine=reader.readLine())!=null){ stringBuilder.append(inputLine); } reader.close(); 返回此.con.toString(); } 捕获(IOE异常){ e、 printStackTrace(); //iam执行get请求事件时在catch块中找到编译器 返回String.format(“url%s\n%s\n%s”,e.getMessage(),this.type,this.url); }最后{ this.con.disconnect(); } } 返回null; },android,Android,我看不到任何Get参数附加到您的web服务中。这一定是原因。您需要随请求发送参数。下面是从代码中提取的代码 public class BackgroundWorker extends AsyncTask<String,Void,String> { public static final int CONNECTION_TIMEOUT=7000; public static final int READ_TIMEOUT=15000; public String
public class BackgroundWorker extends AsyncTask<String,Void,String> {
public static final int CONNECTION_TIMEOUT=7000;
public static final int READ_TIMEOUT=15000;
public String type,username,password,url;
public HttpURLConnection con;
public StringBuilder stringBuilder;
public InputStream input;
public BufferedReader reader;
public URL myURL;
public ProgressDialog pdLoading;
public AlertDialog alertDialog;
public Context context;
BackgroundWorker(Context ctxt){
context = ctxt;
con = null;
}
@Override
protected String doInBackground(String... params) {
this.type = params[0];
this.url = params[1];
this.username = params[2];
this.password = params[3];
if (this.type == "POST")
{
// here iam handle post request
}
if (this.type == "GET")
{
try {
this.myURL = new URL(this.url);
this.con =(HttpURLConnection) this.myURL.openConnection();
this.con.setRequestMethod(this.type);
this.con.setReadTimeout(READ_TIMEOUT);
this.con.setConnectTimeout(CONNECTION_TIMEOUT);
this.con.setRequestProperty("Content-Type", "application/json");
this.con.setRequestProperty("Accept", "application/json");
this.con.connect();
this.input = this.con.getInputStream();
this.reader = new BufferedReader(new InputStreamReader(input,"UTF-8"),8);
stringBuilder = new StringBuilder();
String inputLine;
while((inputLine = reader.readLine()) != null){
stringBuilder.append(inputLine);
}
reader.close();
return this.con.toString();
}
catch(IOException e){
e.printStackTrace();
// compiler found in catch block when iam perform get request event
return String.format("The url %s\n%s\n%s",e.getMessage(),this.type,this.url);
} finally {
this.con.disconnect();
}
}
return null;
}
您没有将这些参数与web服务一起发送。您是否向post man发送了相同的web服务并首先检查了响应显示错误日志您只发布了代码。没有文本解释您想要什么或您做什么。没有问题描述。没有问题。请先写一篇正常的文章。String type=“GET”;String url=“”;BackgroundWorker bW=新的BackgroundWorker(Dashboard.this);bW.execute(type,url,“,”);参数来自此处我知道参数来自何处,但您正在使用Web服务发送这些参数
this.type = params[0];
this.url = params[1];
this.username = params[2];
this.password = params[3];