2014-03-01 2 views
1

저는 웹 웹 요청을 vb.net으로 작성했습니다.이 웹 요청은 Java 웹 요청으로 변환하고, 나머지 서비스는 반환을 요청하며 json에서 매개 변수를 필요로합니다. Newtonsoft를 사용합니다. Json은 vb.net에서 매개 변수를 구성합니다. Java에서 어떻게합니까?vb.net 웹 요청을 java로 변환 HttpwebRequest

Output is the json as in: 
    Dim sw As New StringWriter 
      Dim jsonWriter As JsonWriter = New JsonTextWriter(sw) 
      Dim Output As String 
      jsonWriter.Formatting = Newtonsoft.Json.Formatting.Indented 
        Output = "vehiclecheck?" 
        jsonWriter.WriteStartObject() 
        jsonWriter.WritePropertyName("fault") 
        jsonWriter.WriteValue("1") 
        jsonWriter.WriteEndObject() 
        Output = Output & "where=" & sw.ToString 

       Dim request As HttpWebRequest 
       Dim response As HttpWebResponse = Nothing 
       Dim reader As StreamReader 
       Dim cw As Integer = 0 
       Try 
        If Output = Nothing Then Exit Function 
        request =  DirectCast(WebRequest.Create("https://api.appery.io/rest/1/db/collections/" & myFilter), HttpWebRequest) 
        request.Credentials = New NetworkCredential("user_name", "password") 
        request.Headers.Add("X-Appery-Database-Id:50b3a600e4b0747xxxxxxxx") 
        ' Get response 
        response = DirectCast(request.GetResponse(), HttpWebResponse) 
        reader = New StreamReader(response.GetResponseStream()) 
        Dim results = JsonConvert.DeserializeObject(reader.ReadToEnd()) 

        For R = 0 To results.count - 1 
      'process results 
      next 

나는 당신이 성공하지 못했다고 제공 한 샘플을 사용하려고 시도했다. 내 응용 프로그램에 org.apache.commons.httpclient 라이브러리를 추가하고 다음 클래스를 생성했으며 올바른 URL을 전달하고 있습니다. 제발 조언 해 주시겠습니까?

클래스 RequestTask는 {

@Override 
protected String doInBackground(String... uri) { 
    String username = "username"; 
    String password = "password"; 
    String host = "com.httptest.myapp"; 

    HttpClient client = new HttpClient(); 
    HttpMethod method = new GetMethod(uri[0]); 
    HostConfiguration hostCfg = new HostConfiguration(); 

    HttpState state = client.getState(); 

    method.addRequestHeader("X-Appery-Database-Id", "50b3a600e4b0747d4xxxxxx"); 
    //setting a proxy, for example: 
    state.setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); 
    hostCfg.setProxyHost(new ProxyHost(host, AuthScope.ANY_PORT)); 

    //method.getParams().setSoTimeout(timeout); for example a timeout 

    try { 
     client.executeMethod(hostCfg, method); 
    } catch (HttpException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } //calling the server 
    try { 
     String resp = method.getResponseBodyAsString(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } //the response body 


    return null; 


} 
} 
+0

SO는 코드 변환 서비스가 아니기 때문에이 질문은 논제가 아닌 것처럼 보입니다. – lifetimes

+0

저는 몇 시간 동안이 대화에 어려움을 겪었습니다. 나는 도움이나 포인터를 올바른 방향으로 요청하고 있습니다! – joebohen

답변

0

당신은 httpclient 프레임 워크를 사용할 수 있습니다, 그것은 매우 쉽습니다 AsyncTask를 확장; 몇 가지 예 :

HttpClient client = new HttpClient(); 
HttpMethod method = new GetMethod(URIget); 
HostConfiguration hostCfg = new HostConfiguration(); 

HttpState state = client.getState(); 

//setting a proxy, for example: 
state.setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(getProxyUsername(), getProxyPassword())); 
hostCfg.setProxyHost(new ProxyHost(getProxyHost(), getProxyPort())); 

//method.getParams().setSoTimeout(timeout); for example a timeout 

client.executeMethod(hostCfg, method); //calling the server 
String resp = method.getResponseBodyAsString(); //the response body