2012-06-29 11 views
-1

웹 서비스에 연결하기 위해 URlCaller 클래스의 친구가있는 클래스가 있습니다. 내 가정은 모두 잘 작동하지만 아래에 오류가 J2ME 구현입니다.J2ME URLCaller 클래스를 Android와 동등한 것으로 변환

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package main; 

import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import javax.microedition.io.Connector; 
import javax.microedition.io.HttpConnection; 


public class URLCaller extends Thread{ 
    private String url ; 
    private String action; 
    private URLEncoder urle; 
    private String res; 

    public URLCaller() { 
    } 

    public URLCaller(String action,String url) { 
     urle = new URLEncoder(); 
     this.url = url; 
     this.action = action; 
     start(); 
    } 

    //replace 

void authenticate(String action,String url) { 
HttpConnection connection = null; 
InputStream is = null; 
OutputStream os = null; 
StringBuffer stringBuffer = new StringBuffer(); 

try { 
connection = (HttpConnection)Connector.open(url); 
connection.setRequestMethod(HttpConnection.GET); 
connection.setRequestProperty("IF-Modified-Since","20 Jan 2001 16:19:14 GMT"); 
connection.setRequestProperty("User-Agent","Profile/MIDP-2.0 Confirguration/CLDC-1.0"); 
connection.setRequestProperty("Content-Language", "en-CA"); 
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
os = connection.openOutputStream(); 
is = connection.openDataInputStream(); 
int ch; 
while ((ch = is.read()) != -1) { 
stringBuffer.append((char) ch); 
} 
res = stringBuffer.toString() ; 
System.out.println(res); 
//textBox = new TextBox("Simple GET Test", stringBuffer.toString(), 1024, 0); 

} 
catch(Exception e){ 

} 

finally { 
    try{ 
      if(is!= null) { 
    is.close(); 
} 
if(os != null) { 
    os.close(); 
} 
if(connection != null) { 
    connection.close(); 

} 
//display.setCurrent(textBox); 
    } 
    catch(Exception e){ 

    } 

} 
} 
void sendSMS(String action,String url) { 
HttpConnection connection = null; 
InputStream is = null; 
OutputStream os = null; 
StringBuffer stringBuffer = new StringBuffer(); 
//TextBox textBox = null; 

try { 
connection = (HttpConnection)Connector.open(url); 
connection.setRequestMethod(HttpConnection.GET); 
connection.setRequestProperty("IF-Modified-Since","20 Jan 2001 16:19:14 GMT"); 
connection.setRequestProperty("User-Agent","Profile/MIDP-2.0 Confirguration/CLDC-1.0"); 
connection.setRequestProperty("Content-Language", "en-CA"); 
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
os = connection.openOutputStream(); 
is = connection.openDataInputStream(); 
int ch; 
while ((ch = is.read()) != -1) { 
stringBuffer.append((char) ch); 
} 
res = stringBuffer.toString() ; 
System.out.println(res); 
//textBox = new TextBox("Simple GET Test", stringBuffer.toString(), 1024, 0); 

} 
catch(Exception e){ 

} 

finally { 
    try{ 
      if(is!= null) { 
    is.close(); 
} 
if(os != null) { 
    os.close(); 
} 
if(connection != null) { 
    connection.close(); 

} 
//display.setCurrent(textBox); 
    } 
    catch(Exception e){ 

    } 

} 
} 

    public void run(){ 
     //http://message url?user=mu&password=my&from=Muyiwa&to=23475061254040&message=i+love+this. 
     System.out.println(Thread.currentThread().toString() + " is running...") ; 
     if (action.equals("login")){ 
      System.out.println(action); 

      authenticate(action,url); 
     } 
     else if(action.equals("sendsms")) { 
    System.out.println(action); 

      sendSMS(action,url); 
     } 
    } 

    public void callURL(){ 

     HttpConnection c = null; 
     InputStream is = null; 
     StringBuffer sb = new StringBuffer(); 
     try{ 
System.out.println(url); 
//url = (urle.encode(url,"UTF-8")); 
//System.out.println(url); 
c = (HttpConnection)Connector.open(url, Connector.READ_WRITE, true); 
    c.setRequestMethod(HttpConnection.POST); //default 
    is = c.openInputStream(); // transition to connected! 
    int ch = 0; 
    for(int ccnt=0; ccnt < 150; ccnt++) { // get the title. 
    ch = is.read(); 
    if (ch == -1){ 
     break; 
    } 
    sb.append((char)ch); 

    } 
    res = sb.toString(); 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
    } 

    public String getRes() { 
     return res; 
    } 

    public void setRes(String res) { 
     this.res = res; 
    } 

    public String getUrl() { 
     return url; 
    } 

    public void setUrl(String url) { 
     this.url = url; 
    } 

    public URLEncoder getUrle() { 
     return urle; 
    } 

    public void setUrle(URLEncoder urle) { 
     this.urle = urle; 
    } 




} 

Ps 누군가가이를 Android 구현으로 변환 할 수 있습니까? 현재 최종 기한이 만료 됨

답변

3

android에는 HttpClientDefaultHttpClient 클래스가 있습니다. 그래서 당신은 이것을 사용하여 웹 요청을합니다.

코드와 마찬가지로 HttpGet 요청과 마찬가지로 J2ME 코드와 마찬가지로 스레드 클래스 대신 AsyncTask (스레드, 처리기를 사용할 수도 있지만 Asynctask를 사용하는 것이 좋습니다)을 사용하여 웹 요청을 수행 할 수 있습니다. 비 UI 스레드.

예 :

private class DownloadWebPageTask extends AsyncTask<String, Void, String> { 
     @Override 
     protected String doInBackground(String... urls) { 
      String response = ""; 
      for (String url : urls) { 
       DefaultHttpClient client = new DefaultHttpClient(); 
       HttpGet httpGet = new HttpGet(url); 
       try { 
        HttpResponse execute = client.execute(httpGet); 
        InputStream content = execute.getEntity().getContent(); 

        BufferedReader buffer = new BufferedReader(new InputStreamReader(content)); 
        String s = ""; 
        while ((s = buffer.readLine()) != null) { 
         response += s; 
        } 

       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
      return response; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      textView.setText(result); 
     } 
    } 

이제 안드로이드 활동 코드에서이 DownloadWebPageTask 단지 execute()에 있습니다.

등이

자세한 내용은
DownloadWebPageTask task = new DownloadWebPageTask(); 
task.execute(new String[] { url }); 

그런 좋은 예제 코드이 Tutorial

+0

+1 보면 : – Lucifer