我想将以下python代码转换为java -
def get_ginger_url(text):
"""Get URL for checking grammar using Ginger.
source: https://github.com/zoncoen/python-ginger
@param text English text
@return URL
"""
API_KEY = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
scheme = "http"
netloc = "services.gingersoftware.com"
path = "/Ginger/correct/json/GingerTheText"
params = ""
query = urllib.urlencode([
("lang", "US"),
("clientVersion", "2.0"),
("apiKey", API_KEY),
("text", text)])
fragment = ""
return(urlparse.urlunparse((scheme, netloc, path, params, query, fragment)))
我知道我应该这样做。但我的每一次尝试都失败了。
这是java代码 -
package ginger;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
public class Demo {
public static void main(String[] args) {
try{
//connections settings
String data = "I am eating an apple";
URL url = new URL("http://services.gingersoftware.com/Ginger/correct/json/GingerTheText?lang=US&clientVersion=2.0&apiKey=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX&data=");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoInput(true);
con.setDoOutput (true);
con.setRequestMethod("POST");
//make the request
OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());
writer.write(data);
writer.flush();
//read the request
BufferedReader reader=new BufferedReader(new InputStreamReader(con.getInputStream()));
} catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
我得到的错误是 - 服务器返回HTTP响应代码:URL为
500请帮我转换。
谢谢