More build refinement, HTTP improvements, small info retrieval refactoring

git-svn-id: https://zxing.googlecode.com/svn/trunk@2687 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen@gmail.com 2013-04-24 18:19:36 +00:00
parent 12e78cfa0f
commit 7c6ac647e3
5 changed files with 58 additions and 46 deletions

View file

@ -51,6 +51,8 @@ public final class HttpHelper {
HTML, HTML,
/** JSON content */ /** JSON content */
JSON, JSON,
/** XML */
XML,
/** Plain text content */ /** Plain text content */
TEXT, TEXT,
} }
@ -80,6 +82,9 @@ public final class HttpHelper {
case JSON: case JSON:
contentTypes = "application/json,text/*,*/*"; contentTypes = "application/json,text/*,*/*";
break; break;
case XML:
contentTypes = "application/xml,text/*,*/*";
break;
case TEXT: case TEXT:
default: default:
contentTypes = "text/*,*/*"; contentTypes = "text/*,*/*";
@ -88,22 +93,35 @@ public final class HttpHelper {
} }
private static CharSequence downloadViaHttp(String uri, String contentTypes, int maxChars) throws IOException { private static CharSequence downloadViaHttp(String uri, String contentTypes, int maxChars) throws IOException {
Log.i(TAG, "Downloading " + uri); int redirects = 0;
URL url = new URL(uri); while (redirects < 5) {
HttpURLConnection connection = safelyOpenConnection(url); URL url = new URL(uri);
connection.setRequestProperty("Accept", contentTypes); HttpURLConnection connection = safelyOpenConnection(url);
connection.setRequestProperty("Accept-Charset", "utf-8,*"); connection.setInstanceFollowRedirects(true); // Won't work HTTP -> HTTPS or vice versa
connection.setRequestProperty("User-Agent", "ZXing (Android)"); connection.setRequestProperty("Accept", contentTypes);
try { connection.setRequestProperty("Accept-Charset", "utf-8,*");
int responseCode = safelyConnect(uri, connection); connection.setRequestProperty("User-Agent", "ZXing (Android)");
if (responseCode != HttpURLConnection.HTTP_OK) { try {
throw new IOException("Bad HTTP response: " + responseCode); int responseCode = safelyConnect(uri, connection);
switch (responseCode) {
case HttpURLConnection.HTTP_OK:
return consume(connection, maxChars);
case HttpURLConnection.HTTP_MOVED_TEMP:
String location = connection.getHeaderField("Location");
if (location != null) {
uri = location;
redirects++;
continue;
}
throw new IOException("No Location");
default:
throw new IOException("Bad HTTP response: " + responseCode);
}
} finally {
connection.disconnect();
} }
Log.i(TAG, "Consuming " + uri);
return consume(connection, maxChars);
} finally {
connection.disconnect();
} }
throw new IOException("Too many redirects");
} }
private static String getEncoding(URLConnection connection) { private static String getEncoding(URLConnection connection) {

View file

@ -92,28 +92,9 @@ final class BookResultInfoRetriever extends SupplementalInfoRetriever {
} }
Collection<String> newTexts = new ArrayList<String>(); Collection<String> newTexts = new ArrayList<String>();
maybeAddText(title, newTexts);
if (title != null && title.length() > 0) { maybeAddTextSeries(authors, newTexts);
newTexts.add(title); maybeAddText(pages == null || pages.length() == 0 ? null : pages + "pp.", newTexts);
}
if (authors != null && !authors.isEmpty()) {
boolean first = true;
StringBuilder authorsText = new StringBuilder();
for (String author : authors) {
if (first) {
first = false;
} else {
authorsText.append(", ");
}
authorsText.append(author);
}
newTexts.add(authorsText.toString());
}
if (pages != null && pages.length() > 0) {
newTexts.add(pages + "pp.");
}
String baseBookUri = "http://www.google." + LocaleManager.getBookSearchCountryTLD(context) String baseBookUri = "http://www.google." + LocaleManager.getBookSearchCountryTLD(context)
+ "/search?tbm=bks&source=zxing&q="; + "/search?tbm=bks&source=zxing&q=";

View file

@ -29,6 +29,7 @@ import android.widget.TextView;
import java.io.IOException; import java.io.IOException;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import com.google.zxing.client.android.common.executor.AsyncTaskExecInterface; import com.google.zxing.client.android.common.executor.AsyncTaskExecInterface;
@ -143,4 +144,26 @@ public abstract class SupplementalInfoRetriever extends AsyncTask<Object,Object,
newHistories.add(new String[] {itemID, newText}); newHistories.add(new String[] {itemID, newText});
} }
static void maybeAddText(String text, Collection<String> texts) {
if (text != null && text.length() > 0) {
texts.add(text);
}
}
static void maybeAddTextSeries(Collection<String> textSeries, Collection<String> texts) {
if (textSeries != null && !textSeries.isEmpty()) {
boolean first = true;
StringBuilder authorsText = new StringBuilder();
for (String author : textSeries) {
if (first) {
first = false;
} else {
authorsText.append(", ");
}
authorsText.append(author);
}
texts.add(authorsText.toString());
}
}
} }

View file

@ -166,12 +166,6 @@
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId> <artifactId>maven-surefire-plugin</artifactId>
<version>2.14</version> <version>2.14</version>
<configuration>
<!--<parallel>classes</parallel>-->
<systemPropertyVariables>
<explicitLuminanceConversion>true</explicitLuminanceConversion>
</systemPropertyVariables>
</configuration>
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>

View file

@ -37,10 +37,6 @@
<servlet-name>DecodeServlet</servlet-name> <servlet-name>DecodeServlet</servlet-name>
<url-pattern>/w/decode</url-pattern> <url-pattern>/w/decode</url-pattern>
</servlet-mapping> </servlet-mapping>
<servlet-mapping>
<servlet-name>jsp</servlet-name>
<url-pattern>*.jspx</url-pattern>
</servlet-mapping>
<welcome-file-list> <welcome-file-list>
<welcome-file>index.jspx</welcome-file> <welcome-file>index.jspx</welcome-file>