如何使用Java RESTful客户端使用Wisetrend OCR Web服务? 导入java.io.BufferedWriter; 导入java.io.FileWriter; 导入javax.xml.parsers.DocumentBuilder; 导入org.apache.http.HttpHost; 导入org.apache.http.HttpResponse; 导入org.apache.http.client.methods.HttpPost; 导入org.apache.http.client.params.ClientPNames; 导入org.apache.http.client.params.CookiePolicy; 导入org.apache.http.conn.params.ConnRoutePNames; 导入org.apache.http.entity.StringEntity; 导入org.apache.http.impl.client.DefaultHttpClient; 导入org.apache.http.params.BasicHttpParams; 导入org.apache.http.params.HttpConnectionParams; 导入org.apache.http.params.HttpParams; 导入org.apache.http.util.EntityUtils; 公共类客户端{ 公共静态字符串url=”http://www.wisetrend.com/WiseTREND_Online_OCR_API_v2.0.htm"; 专用静态int proxyPort=8000; 私有静态字符串proxyHost=“proxyHost”; 静态saxsp; 公共静态void main(字符串[]args){ OCRRestClient=新的OCRRestClient(); /*client.getConvertedText(); } public void getConvertedText(){*/ HttpParams myParams=新的BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(myParams,10000); HttpConnectionParams.setSoTimeout(myParams,10000); DefaultHttpClient httpClient=新的DefaultHttpClient(myParams); HttpHost proxy=新的HttpHost(proxyHost,proxyPort); httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,PROXY); 试一试{ 字符串ret=null; httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY,CookiePolicy.RFC_2109); 字符串键=“****”;//我的键 字符串imageURL=”http://www.androidpeople.com/wp-content/themes/bp-columns/images/android_logo.png"; HttpResponse响应=null; HttpPost HttpPost=新的HttpPost(“http://svc.webservius.com/v1/wisetrend/wiseocr/submit?wsvKey=“+键); setHeader(“内容类型”、“文本/xml”); httppost.setEntity(新的StringEntity(“”)+ “”+imageURL+“”+ "")); response=httpClient.execute(httppost); System.out.println(response.toString()); while(true){ sp=新的SAXParser(); if(响应!=null){ ret=EntityUtils.toString(response.getEntity()); System.out.println(“响应:+ret”); int success=writeFile(ret,“c://out.xml”); 如果(成功==1) { //使用SAX解析器解析文档。 sp.parseDocument(“c://out.xml”); 系统输出打印项次(“最终状态:+sp状态”); 如果(sp.status.equalsIgnoreCase(“完成”)){ 系统输出打印项次(“完成状态:+sp状态”); } } } } }捕获(例外e){ e、 printStackTrace(); } } 公共静态int-writeFile(字符串内容、字符串文件名) { 试一试{ //创建文件 FileWriter fstream=新的FileWriter(文件名); BufferedWriter out=新的BufferedWriter(fstream); 写出(内容); //关闭输出流 out.close(); 返回1; }catch(异常e){//catch异常(如果有) System.err.println(“错误:+e.getMessage()); 返回0; } } }

如何使用Java RESTful客户端使用Wisetrend OCR Web服务? 导入java.io.BufferedWriter; 导入java.io.FileWriter; 导入javax.xml.parsers.DocumentBuilder; 导入org.apache.http.HttpHost; 导入org.apache.http.HttpResponse; 导入org.apache.http.client.methods.HttpPost; 导入org.apache.http.client.params.ClientPNames; 导入org.apache.http.client.params.CookiePolicy; 导入org.apache.http.conn.params.ConnRoutePNames; 导入org.apache.http.entity.StringEntity; 导入org.apache.http.impl.client.DefaultHttpClient; 导入org.apache.http.params.BasicHttpParams; 导入org.apache.http.params.HttpConnectionParams; 导入org.apache.http.params.HttpParams; 导入org.apache.http.util.EntityUtils; 公共类客户端{ 公共静态字符串url=”http://www.wisetrend.com/WiseTREND_Online_OCR_API_v2.0.htm"; 专用静态int proxyPort=8000; 私有静态字符串proxyHost=“proxyHost”; 静态saxsp; 公共静态void main(字符串[]args){ OCRRestClient=新的OCRRestClient(); /*client.getConvertedText(); } public void getConvertedText(){*/ HttpParams myParams=新的BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(myParams,10000); HttpConnectionParams.setSoTimeout(myParams,10000); DefaultHttpClient httpClient=新的DefaultHttpClient(myParams); HttpHost proxy=新的HttpHost(proxyHost,proxyPort); httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,PROXY); 试一试{ 字符串ret=null; httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY,CookiePolicy.RFC_2109); 字符串键=“****”;//我的键 字符串imageURL=”http://www.androidpeople.com/wp-content/themes/bp-columns/images/android_logo.png"; HttpResponse响应=null; HttpPost HttpPost=新的HttpPost(“http://svc.webservius.com/v1/wisetrend/wiseocr/submit?wsvKey=“+键); setHeader(“内容类型”、“文本/xml”); httppost.setEntity(新的StringEntity(“”)+ “”+imageURL+“”+ "")); response=httpClient.execute(httppost); System.out.println(response.toString()); while(true){ sp=新的SAXParser(); if(响应!=null){ ret=EntityUtils.toString(response.getEntity()); System.out.println(“响应:+ret”); int success=writeFile(ret,“c://out.xml”); 如果(成功==1) { //使用SAX解析器解析文档。 sp.parseDocument(“c://out.xml”); 系统输出打印项次(“最终状态:+sp状态”); 如果(sp.status.equalsIgnoreCase(“完成”)){ 系统输出打印项次(“完成状态:+sp状态”); } } } } }捕获(例外e){ e、 printStackTrace(); } } 公共静态int-writeFile(字符串内容、字符串文件名) { 试一试{ //创建文件 FileWriter fstream=新的FileWriter(文件名); BufferedWriter out=新的BufferedWriter(fstream); 写出(内容); //关闭输出流 out.close(); 返回1; }catch(异常e){//catch异常(如果有) System.err.println(“错误:+e.getMessage()); 返回0; } } },android,mobile,ocr,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.a

我在尝试访问此文件时遇到以下错误

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
答复:http://api.ocr-it.com/ocr/v2/getStatus/f78377d5bdcd44d18cafe65c253f87e6Submitted
Strt:状态:
结束:状态:已提交
java.lang.IllegalStateException:内容已被消费
位于org.apache.http.entity.BasicHttpEntity.getContent(BasicHttpEntity.java:84)
位于org.apache.http.conn.BasicManagedEntity.getContent(BasicManagedEntity.java:87)
位于org.apache.http.util.EntityUtils.toString(EntityUtils.java:138)
位于org.apache.http.util.EntityUtils.toString(EntityUtils.java:183)
位于OCRRestClient.main(OCRRestClient.java:62)
最后状态:已提交
请帮我整理一下。我在哪里失踪?在哪里更改我的代码

在wise趋势文档页面中提供的c代码的帮助下,我产生了这段代码


有问题的行是System.out.println(response.toString())。这基本上只读取一次响应。由于它直接脱离导线,因此无法重新读取。移除SOP,它应该可以工作

有问题的行是System.out.println(response.toString())。这基本上只读取一次响应。由于它直接脱离导线,因此无法重新读取。移除SOP,它应该可以工作

我忘了发送
GET
请求
JobUrl

它现在运转良好

下面的代码将执行所需的操作

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

我忘记发送
GET
请求
JobUrl

它现在运转良好

下面的代码将执行所需的操作

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

看起来,您还有一个无限循环,因为有一个while(true)没有退出条件。看起来,您还有一个无限循环,因为有一个while(true)没有退出条件。