Open GoogleCodeExporter opened 9 years ago
I have same problem...(
Android 4.0.3
Original comment by infil...@gmail.com
on 8 Apr 2012 at 1:32
the same
Original comment by karandin...@gmail.com
on 10 Apr 2012 at 10:02
Removing line 86 in com.google.api.GoogleAPI worked for me:
//connection.setDoOutput(true);
4.0 thinks with this line it should definitely be POST method.
More info here: http://webdiary.com/2011/12/14/ics-get-post/
Original comment by karandin...@gmail.com
on 11 Apr 2012 at 12:31
Hi I have solved my problem by creating my own function to translate
and explain
the google api. I show you my code. sorry for my English.
I send you an example with a EditText you enter a word in Spanish and
translates
it into English you when you press the button.
Hope this helps
//EXAMPLE
package com.prueba.traductor;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class EjemploTraductorJSONActivity extends Activity {
String result = "";
final String tag = "Prueba";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText txtSearch = (EditText) findViewById(R.id.etTexto);
final Button btnSearch = (Button) findViewById(R.id.btTraducir);
btnSearch.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
String txtquery = txtSearch.getText().toString();
callGoogleTranslate("es", "en", txtquery);
}
});
} // end onCreate()
public static String callGoogleTranslate(String fromLanguage,
String toLanguage, String textToTranslate) {
String yourKey = "asdasdasf34124124124124";
String result = null;
String URL = "https://www.googleapis.com/language/translate/v2";
String key = "?key=" + yourKey;
String sourceParam = "&source=" + fromLanguage;
String toParam = "&target=" + toLanguage;
String textParam = "&q=" + textToTranslate.replaceAll(" ", "%20");
String fullURL = URL + key + sourceParam + toParam + textParam;
System.out.println(fullURL);
HttpClient httpClient = new DefaultHttpClient();
HttpGet del = new HttpGet(fullURL);
HttpResponse resp;
try {
resp = httpClient.execute(del);
String respStr = EntityUtils.toString(resp.getEntity());
// Parse Result http
proccesResult(respStr);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
static String proccesResult(String jsonStringData) {
try {
JSONObject data = new JSONObject(jsonStringData);
JSONObject jsoObj2 = data.getJSONObject("data");
JSONArray jArray = jsoObj2.getJSONArray("translations");
JSONObject steps = jArray.getJSONObject(0);
String txtTraducido = steps.getString("translatedText");
// TRANSLATE TEXT
Log.i("Prueba", "Resultado-> " + txtTraducido);
return txtTraducido;
} catch (JSONException e) {
e.printStackTrace();
}
return "";
}
}
Original comment by cristian...@gmail.com
on 19 Apr 2012 at 6:55
I changed the method to work better because it gives errors when passing the
url HTTPGet
public static String callGoogleTranslate(String fromLanguage, String
toLanguage, String textToTranslate){
String result = null;
String apiKey="yourApikey";
String textParam=textToTranslate;
Uri uri = new Uri.Builder()
.scheme("https")
.authority("www.googleapis.com")
.path("language/translate/v2")
.appendQueryParameter("key", apiKey)
.appendQueryParameter("source", fromLanguage)
.appendQueryParameter("target", toLanguage)
.appendQueryParameter("q",textParam )
.build();
HttpClient httpClient = new DefaultHttpClient();
HttpGet del =new HttpGet(uri.toString());
HttpResponse resp;
try {
resp = httpClient.execute(del);
String respStr =
EntityUtils.toString(resp.getEntity(),"ISO-8859-1");
result=proccesResult(respStr);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
2012/4/19 cristian paciencia verdu <cristianpv69@gmail.com>
Original comment by cristian...@gmail.com
on 20 Apr 2012 at 8:02
Thanks for sharing !
Original comment by eosinoph...@gmail.com
on 25 May 2012 at 1:28
[deleted comment]
how get API translator key ????????
Original comment by bharadva...@gmail.com
on 15 Mar 2013 at 1:03
Any fix?
Original comment by mickmeta...@gmail.com
on 28 Mar 2013 at 5:35
i tried the above code but am unable to get output..i am attaching error log
build.
plz anyone solve the issue .
Original comment by prema.m...@gmail.com
on 25 Feb 2014 at 9:42
Attachments:
[deleted comment]
Thank you for sharing this usefull information #4 #5 cristian...@gmail.com
Original comment by ashishdd...@gmail.com
on 4 Jul 2014 at 10:07
#10 try run on thread web service and check in your manifest.xml internet
permission.
Original comment by cristian...@gmail.com
on 2 Dec 2014 at 6:45
Original issue reported on code.google.com by
cristian...@gmail.com
on 8 Mar 2012 at 8:25