如何使用Java RESTful客户端使用Wisetrend OCR Web服务?

时间:2011-02-25 07:45:56

标签: android mobile ocr

import java.io.BufferedWriter;
import java.io.FileWriter;

import javax.xml.parsers.DocumentBuilder;

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;

import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;


public class OCRRestClient {
public static String url =  "http://www.wisetrend.com/WiseTREND_Online_OCR_API_v2.0.htm";
private static int proxyPort=8000;
private static String proxyHost="ProxyHost";

static SAXParser sp;
public static void main(String[] args) {
    OCRRestClient client = new OCRRestClient();
/*  client.getConvertedText();
}

public void getConvertedText(){*/
    HttpParams myParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(myParams, 10000);
    HttpConnectionParams.setSoTimeout(myParams, 10000);
    DefaultHttpClient httpClient = new DefaultHttpClient(myParams);

    HttpHost proxy = new HttpHost(proxyHost, proxyPort);
    httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);

    try {
        String ret = null;
        httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY,CookiePolicy.RFC_2109);
        String key = "****"; // my key
        String imageURL = "http://www.androidpeople.com/wp-content/themes/bp-columns/images/android_logo.png";
        HttpResponse response  = null;

        HttpPost httppost = new HttpPost("http://svc.webservius.com/v1/wisetrend/wiseocr/submit?wsvKey="+key);
        httppost.setHeader("Content-Type","text/xml");

        httppost.setEntity(new StringEntity("<Job>" +
                                                "<InputURL>"+imageURL+"</InputURL>"+
                                             "</Job>"));
        response = httpClient.execute(httppost );
        System.out.println(response.toString());

        while(true){
            sp = new SAXParser();
        if (response != null) {
            ret = EntityUtils.toString(response.getEntity());
            System.out.println("Response: "+ret);
            int success = writeFile(ret, "c://out.xml");
            if(success == 1)
            {
                //parsing the document using SAX Parser.
                sp.parseDocument("c://out.xml");
                System.out.println("Final status : "+sp.status);
                if(sp.status.equalsIgnoreCase("Finished")){
                    System.out.println("Finished status : "+sp.status);
                }
            }


      }

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

public static int writeFile(String content, String filename)
{
    try{
        // Create file 
        FileWriter fstream = new FileWriter(filename);
        BufferedWriter out = new BufferedWriter(fstream);
        out.write(content);
        //Close the output stream
        out.close();
        return 1;
    }catch (Exception e){//Catch exception if any
        System.err.println("Error: " + e.getMessage());
        return 0;
    }
}

}

我在尝试访问此错误时收到以下错误。

org.apache.http.message.BasicHttpResponse@1837697
Response: <JobStatus xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><JobURL>http://api.ocr-it.com/ocr/v2/getStatus/f78377d5bdcd44d18cafe65c253f87e6</JobURL><Status>Submitted</Status></JobStatus>
Strt : Status: 
End : Status: Submitted
java.lang.IllegalStateException: Content has been consumed
    at org.apache.http.entity.BasicHttpEntity.getContent(BasicHttpEntity.java:84)
    at org.apache.http.conn.BasicManagedEntity.getContent(BasicManagedEntity.java:87)
    at org.apache.http.util.EntityUtils.toString(EntityUtils.java:138)
    at org.apache.http.util.EntityUtils.toString(EntityUtils.java:183)
    at OCRRestClient.main(OCRRestClient.java:62)
Final status : Submitted

请帮我解决这个问题。我错过了哪里?在哪里更改我的代码?

我已经在智能趋势文档页面中提供的c#代码的帮助下提出了这段代码。

http://www.wisetrend.com/WiseTREND_Online_OCR_API_v2.0.htm

2 个答案:

答案 0 :(得分:1)

违规行是System.out.println(response.toString()); 。这实际上是一次读取响应。由于它直接脱离电线,因此无法重读。 Remo SOP它应该工作。

答案 1 :(得分:0)

我忘了发送GET的{​​{1}}请求。

现在工作正常。

以下代码可以满足需要。

JobUrl