2017-12-12 21 views
1
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; 
    } 
+1

あなたは同じWebサービスを利用して人を投稿し、最初に応答を確認しました –

+0

エラーログを表示 –

+0

あなたはコードを投稿しました。あなたが何を望んでいるのか、何をしているのかを説明するテキストはありません。問題の説明はありません。疑問はない。最初に普通のまともな投稿を書いてください。 – greenapps

答えて

1

Webサービスに追加されているパラメータ取得が表示されません。これが理由でなければならない。あなたはrequrestでパラメーターを送信する必要があります。以下はコードから取得したコードです

this.type = params[0]; 
    this.url = params[1]; 
    this.username = params[2]; 
    this.password = params[3]; 

あなたのWebサービスでこれらのパラメータを送信していません。

+0

文字列型= "GET"; String url = "http://7eb128fa.ngrok.io/courses"; BackgroundWorker bW =新しいBackgroundWorker(Dashboard.this); bW.execute(type、url、 ""、 ""); –

+0

パラメータはここから来ます –

+0

私はパラメータがどこから来ているのか知っていますが、それらのパラメータをWebサービスで送信しています –

関連する問題