2017-12-12 1 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

같은 웹 서비스를 사용하여 사람을 게시하고 응답을 먼저 확인 했습니까 –

+0

오류 로그를 표시하십시오 –

+0

코드 만 게시했습니다. 당신이 원하는 것을 설명하거나 당신이하는 것을 설명하는 텍스트가 없습니다. 문제에 대한 설명은 없습니다. 의심의 여지가 없습니다. 먼저 정상적인 알맞은 글을 써주세요. – greenapps

답변

1

웹 서비스에 추가 매개 변수 가져 오기가 표시되지 않습니다. 이것이 이유가되어야합니다. 당신은 requrest와 매개 변수를 보내야합니다. 코드에서 가져온 코드는 다음과 같습니다.

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

웹 서비스에는 이러한 매개 변수가 전송되지 않습니다.

+0

문자열 유형 = "가져 오기"; String url = "http://7eb128fa.ngrok.io/courses"; BackgroundWorker bW = 새 BackgroundWorker (Dashboard.this); bW.execute (type, url, "", ""); –

+0

여기에서 매개 변수가 나온다 –

+0

매개 변수의 출처를 알고 있지만 웹 서비스와 함께 매개 변수를 보내고있다. –

관련 문제