use of org.apache.http.ParseException in project android-app by eoecn.
the class CustomHttpClient method getFromWebByHttpClient.
public static String getFromWebByHttpClient(Context context, String url, NameValuePair... nameValuePairs) throws Exception {
log.d("getFromWebByHttpClient url = " + url);
try {
// http地址
// String httpUrl =
// "http://192.168.1.110:8080/httpget.jsp?par=HttpClient_android_Get";
StringBuilder sb = new StringBuilder();
sb.append(url);
if (nameValuePairs != null && nameValuePairs.length > 0) {
sb.append("?");
for (int i = 0; i < nameValuePairs.length; i++) {
if (i > 0) {
sb.append("&");
}
sb.append(String.format("%s=%s", nameValuePairs[i].getName(), nameValuePairs[i].getValue()));
}
}
// HttpGet连接对象
HttpGet httpRequest = new HttpGet(sb.toString());
// 取得HttpClient对象
HttpClient httpclient = getHttpClient(context);
// 请求HttpClient,取得HttpResponse
HttpResponse httpResponse = httpclient.execute(httpRequest);
// 请求成功
if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
throw new RuntimeException(context.getResources().getString(R.string.httpError));
}
return EntityUtils.toString(httpResponse.getEntity());
} catch (ParseException e) {
// TODO Auto-generated catch block
throw new RuntimeException(context.getResources().getString(R.string.httpError), e);
} catch (IOException e) {
// TODO Auto-generated catch block
log.e("IOException ");
e.printStackTrace();
throw new RuntimeException(context.getResources().getString(R.string.httpError), e);
}
}
use of org.apache.http.ParseException in project robovm by robovm.
the class AbstractMessageParser method parse.
public HttpMessage parse() throws IOException, HttpException {
HttpMessage message = null;
try {
message = parseHead(this.sessionBuffer);
} catch (ParseException px) {
throw new ProtocolException(px.getMessage(), px);
}
Header[] headers = AbstractMessageParser.parseHeaders(this.sessionBuffer, this.maxHeaderCount, this.maxLineLen, this.lineParser);
message.setHeaders(headers);
return message;
}
use of org.apache.http.ParseException in project robovm by robovm.
the class BasicLineParser method parseStatusLine.
// non-javadoc, see interface LineParser
public StatusLine parseStatusLine(final CharArrayBuffer buffer, final ParserCursor cursor) throws ParseException {
if (buffer == null) {
throw new IllegalArgumentException("Char array buffer may not be null");
}
if (cursor == null) {
throw new IllegalArgumentException("Parser cursor may not be null");
}
int indexFrom = cursor.getPos();
int indexTo = cursor.getUpperBound();
try {
// handle the HTTP-Version
ProtocolVersion ver = parseProtocolVersion(buffer, cursor);
// handle the Status-Code
skipWhitespace(buffer, cursor);
int i = cursor.getPos();
int blank = buffer.indexOf(' ', i, indexTo);
if (blank < 0) {
blank = indexTo;
}
int statusCode = 0;
try {
statusCode = Integer.parseInt(buffer.substringTrimmed(i, blank));
} catch (NumberFormatException e) {
throw new ParseException("Unable to parse status code from status line: " + buffer.substring(indexFrom, indexTo));
}
//handle the Reason-Phrase
i = blank;
String reasonPhrase = null;
if (i < indexTo) {
reasonPhrase = buffer.substringTrimmed(i, indexTo);
} else {
reasonPhrase = "";
}
return createStatusLine(ver, statusCode, reasonPhrase);
} catch (IndexOutOfBoundsException e) {
throw new ParseException("Invalid status line: " + buffer.substring(indexFrom, indexTo));
}
}
use of org.apache.http.ParseException in project robovm by robovm.
the class BasicLineParser method parseProtocolVersion.
// non-javadoc, see interface LineParser
public ProtocolVersion parseProtocolVersion(final CharArrayBuffer buffer, final ParserCursor cursor) throws ParseException {
if (buffer == null) {
throw new IllegalArgumentException("Char array buffer may not be null");
}
if (cursor == null) {
throw new IllegalArgumentException("Parser cursor may not be null");
}
final String protoname = this.protocol.getProtocol();
final int protolength = protoname.length();
int indexFrom = cursor.getPos();
int indexTo = cursor.getUpperBound();
skipWhitespace(buffer, cursor);
int i = cursor.getPos();
// long enough for "HTTP/1.1"?
if (i + protolength + 4 > indexTo) {
throw new ParseException("Not a valid protocol version: " + buffer.substring(indexFrom, indexTo));
}
// check the protocol name and slash
boolean ok = true;
for (int j = 0; ok && (j < protolength); j++) {
ok = (buffer.charAt(i + j) == protoname.charAt(j));
}
if (ok) {
ok = (buffer.charAt(i + protolength) == '/');
}
if (!ok) {
throw new ParseException("Not a valid protocol version: " + buffer.substring(indexFrom, indexTo));
}
i += protolength + 1;
int period = buffer.indexOf('.', i, indexTo);
if (period == -1) {
throw new ParseException("Invalid protocol version number: " + buffer.substring(indexFrom, indexTo));
}
int major;
try {
major = Integer.parseInt(buffer.substringTrimmed(i, period));
} catch (NumberFormatException e) {
throw new ParseException("Invalid protocol major version number: " + buffer.substring(indexFrom, indexTo));
}
i = period + 1;
int blank = buffer.indexOf(' ', i, indexTo);
if (blank == -1) {
blank = indexTo;
}
int minor;
try {
minor = Integer.parseInt(buffer.substringTrimmed(i, blank));
} catch (NumberFormatException e) {
throw new ParseException("Invalid protocol minor version number: " + buffer.substring(indexFrom, indexTo));
}
cursor.updatePos(blank);
return createProtocolVersion(major, minor);
}
use of org.apache.http.ParseException in project robovm by robovm.
the class BasicTokenIterator method findTokenStart.
/**
* Determines the starting position of the next token.
* This method will iterate over headers if necessary.
*
* @param from the position in the current header at which to
* start the search
*
* @return the position of the token start in the current header,
* negative if no token start could be found
*/
protected int findTokenStart(int from) {
if (from < 0) {
throw new IllegalArgumentException("Search position must not be negative: " + from);
}
boolean found = false;
while (!found && (this.currentHeader != null)) {
final int to = this.currentHeader.length();
while (!found && (from < to)) {
final char ch = this.currentHeader.charAt(from);
if (isTokenSeparator(ch) || isWhitespace(ch)) {
// whitspace and token separators are skipped
from++;
} else if (isTokenChar(this.currentHeader.charAt(from))) {
// found the start of a token
found = true;
} else {
throw new ParseException("Invalid character before token (pos " + from + "): " + this.currentHeader);
}
}
if (!found) {
if (this.headerIt.hasNext()) {
this.currentHeader = this.headerIt.nextHeader().getValue();
from = 0;
} else {
this.currentHeader = null;
}
}
}
return found ? from : -1;
}
Aggregations