2012-06-29 42 views
-1

我從朋友那裏獲得了一個URlCaller類的類來連接到web服務。我的假設是,所有將工作做好,但它包含下面的錯誤是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; 
    } 




} 

詩會有人將它轉換爲一個機器人的實現。目前面臨限期

回答

3

在android中有類HttpClientDefaultHttpClient。因此,您使用這些請求發出Web請求。

與您的代碼做一個HttpGet要求,也是像你的J2ME代碼,而不是Thread類的,你可以使用AsyncTask(您也可以使用線程,處理器卻的AsyncTask是更好地使用)執行Web請求非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); 
     } 
    } 

現在從Android活動代碼,你必須只​​這個DownloadWebPageTask

一樣,

DownloadWebPageTask task = new DownloadWebPageTask(); 
task.execute(new String[] { url }); 

欲瞭解更多信息,看看這個Tutorial

+0

+1這樣一個很好的例子代碼:) – Lucifer

相關問題