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,
/** JSON content */
JSON,
/** XML */
XML,
/** Plain text content */
TEXT,
}
@ -80,6 +82,9 @@ public final class HttpHelper {
case JSON:
contentTypes = "application/json,text/*,*/*";
break;
case XML:
contentTypes = "application/xml,text/*,*/*";
break;
case TEXT:
default:
contentTypes = "text/*,*/*";
@ -88,22 +93,35 @@ public final class HttpHelper {
}
private static CharSequence downloadViaHttp(String uri, String contentTypes, int maxChars) throws IOException {
Log.i(TAG, "Downloading " + uri);
URL url = new URL(uri);
HttpURLConnection connection = safelyOpenConnection(url);
connection.setRequestProperty("Accept", contentTypes);
connection.setRequestProperty("Accept-Charset", "utf-8,*");
connection.setRequestProperty("User-Agent", "ZXing (Android)");
try {
int responseCode = safelyConnect(uri, connection);
if (responseCode != HttpURLConnection.HTTP_OK) {
throw new IOException("Bad HTTP response: " + responseCode);
int redirects = 0;
while (redirects < 5) {
URL url = new URL(uri);
HttpURLConnection connection = safelyOpenConnection(url);
connection.setInstanceFollowRedirects(true); // Won't work HTTP -> HTTPS or vice versa
connection.setRequestProperty("Accept", contentTypes);
connection.setRequestProperty("Accept-Charset", "utf-8,*");
connection.setRequestProperty("User-Agent", "ZXing (Android)");
try {
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) {

View file

@ -92,28 +92,9 @@ final class BookResultInfoRetriever extends SupplementalInfoRetriever {
}
Collection<String> newTexts = new ArrayList<String>();
if (title != null && title.length() > 0) {
newTexts.add(title);
}
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.");
}
maybeAddText(title, newTexts);
maybeAddTextSeries(authors, newTexts);
maybeAddText(pages == null || pages.length() == 0 ? null : pages + "pp.", newTexts);
String baseBookUri = "http://www.google." + LocaleManager.getBookSearchCountryTLD(context)
+ "/search?tbm=bks&source=zxing&q=";

View file

@ -29,6 +29,7 @@ import android.widget.TextView;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import com.google.zxing.client.android.common.executor.AsyncTaskExecInterface;
@ -142,5 +143,27 @@ public abstract class SupplementalInfoRetriever extends AsyncTask<Object,Object,
newContents.add(content);
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>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14</version>
<configuration>
<!--<parallel>classes</parallel>-->
<systemPropertyVariables>
<explicitLuminanceConversion>true</explicitLuminanceConversion>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>

View file

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