Skip to content

Commit

Permalink
java용 Custom Search 실험중
Browse files Browse the repository at this point in the history
  • Loading branch information
surcae committed Jun 11, 2018
1 parent cb0ca69 commit 562396e
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 8 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ dependencies {
compile 'com.google.firebase:firebase-auth:11.2.0'
compile 'com.google.android.gms:play-services-auth:11.2.0'
compile 'com.firebase:firebase-client-android:2.5.0'
compile 'com.google.apis:google-api-services-customsearch:v1-rev63-1.23.0'
}
apply plugin: 'com.google.gms.google-services'
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@
import android.os.AsyncTask;
import android.util.Log;

import com.fasterxml.jackson.core.JsonFactory;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.services.customsearch.Customsearch;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

Expand All @@ -25,20 +34,83 @@
*/

public class ImageSearchEngine extends AsyncTask<URL, Integer, String> {
@Override
Integer responseCode = null;
String responseMessage = "";
String result = "";
HttpURLConnection conn = null;
Customsearch customsearch= null;
// Callback from Execute() of MainActivity.
@Override
protected void onPreExecute(){

}

protected String doInBackground(URL... urls) {
URL url = urls[0];
HttpURLConnection conn = null;
HttpTransport httpTransport;
JsonFactory jsonFactory;
HttpRequestInitializer httpRequestInitializer;

try{
//customsearch = new Customsearch(new NetHttpTransport(), new JsonFactory(), new HttpRequestInitializer()
{

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

URL url = urls[0];
try {
conn = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
Log.e(TAG, "Http connection ERROR " + e.toString());
}
return null;
}
protected void onPreExecute(){

try {
responseCode = conn.getResponseCode();
responseMessage = conn.getResponseMessage();
} catch (IOException e) {
Log.e(TAG, "Http getting response code ERROR " + e.toString());
}

Log.d(TAG, "Http response code =" + responseCode + " message=" + responseMessage);

try {
if (responseCode == 200) {

// response OK
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;

while ((line = rd.readLine()) != null) {
sb.append(line + "\n");
}
rd.close();

conn.disconnect();

result = sb.toString();

Log.d(TAG, "result=" + result);

return result;

} else {

// response problem

String errorMsg = "Http ERROR response " + responseMessage + "\n" + "Make sure to replace in code your own Google API key and Search Engine ID";
Log.e(TAG, errorMsg);
result = errorMsg;
return result;

}
} catch (IOException e) {
Log.e(TAG, "Http Response ERROR " + e.toString());
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ public boolean onQueryTextSubmit(String query) {
}
Log.d("MainActivity", "Url = "+ urlString);


imageSearchEngine = new ImageSearchEngine();
imageSearchEngine.execute(url);
return true;
}

Expand Down
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ buildscript {

repositories {
google()
mavenCentral()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
classpath 'com.android.tools.build:gradle:3.1.3'


// NOTE: Do not place your application dependencies here; they belong
Expand Down

0 comments on commit 562396e

Please sign in to comment.