class MySync extends AsyncTask{ ProgressDialog mProgressDialog;
protected void onPreExecute(){
mProgressDialog = ProgressDialog.show(MainActivity.this, "Loading...", "Data is Loading...");
}
@Override
protected Integer doInBackground(String... params) {
int result = 0;
//String url="http://192.168.0.108:8080/sbi/login?"+"key1="+params[0]+"&key2="+params[1]";
int code;
try {
URL hp=new URL("http://192.168.0.108:8080/sbi/login?"+"key1="+params[0]+"&key2="+params[1]);
HttpURLConnection urlConnection=(HttpURLConnection)hp.openConnection();
urlConnection.connect();
Log.i("A", "connect");
code=urlConnection.getResponseCode();
Log.i("A","code");
boolean a=check(code);
if(a){
//urlConnection.setDoInput(true);
Log.i("A", "input");
// urlConnection.setDoOutput(true);
Log.i("A", "output");
urlConnection.setRequestMethod("GET");
Log.i("A", "get");
byte [] buf=("key1=" + params[0] + "&key2=" + params[1]).getBytes();
urlConnection.getOutputStream().write(buf);
Log.i("A", "sent");
}
else{
Log.i("A","error");
result=3;
}
}
catch (MalformedURLException e) {
Log.i("e", "Error");
}
catch (IOException e){
e.printStackTrace();
}
protected boolean check(int c){
if(c==200) return true;
else return false;
}
}
该代码报错“方法不支持请求主体:get?”,如果我插入 setdooutput(true) ,那么它会报错“已连接”。我是 Android 的新手,正在制作我的大学项目。
urlConnection.getOutputStream().write(buf);
并尝试不使用setDoOutput
,会发生什么? - Shree Krishna